Hello. I'm trying to make tiny helper method for simplify routine operations But on the sample:
public static int getEntityId<Type, Entity>(String name) where Entity: class
{
Type type = _db.GetTable<Entity>().SingleOrDefault(t => t.name == name);
return 0;
}
i get error:
Error 1 'Entity' does not contain a definition for 'name' and no extension method 'name' accepting a first argument of type 'Entity' could be found (are you missing a using directive or an assembly reference?) c:\work\asutp_migration\asutp_migration\Program.cs 89 62 asutp_migration
Actually, this is expected error, but how to solve this? All tables/classes i will use with this method have "name" field/property.
UPD 1: according to advices i did this:
public partial class unit : IHasName
{
}
interface IHasName
{
string name { get; }
int id { get; }
}
public static int getEntityId<Type, Entity>(String name) where Entity: class, IHasName
{
Type type = _db.GetTable<Entity>().SingleOrDefault(t => t.name == name);
return (type == null) ? type.id : 0;
}
and with code:
int i = getEntityId<unit>("м");
Console.WriteLine(i);
i get exception:
Unhandled Exception: System.NotSupportedException: The mapping of interface member IHasName.name is not supported.
UPD 2: http://connect.microsoft.com/VisualStudio/feedback/details/344903/linq-to-sql-mapping-interface-member-not-supported-exception i can't believe. is it related to my issue?
UPD 3: yeah, seems it's VS issue: http://social.msdn.microsoft.com/Forums/en/linqtosql/thread/bc2fbbce-eb63-4735-9b2d-26b4ab8fe589
UPD 4:
public static int getEntityId<Type>(String name) where Type: class, IHasName
{
Type type = _db.GetTable<Type>().SingleOrDefault(t => t.name.Equals(name));
return (type != null) ? type.id : 0;
}
so this is a solution, thank you all :-)