C# generics with linq search criteria
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
A开发者_如何学Pythonctually, 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 :-)
Well, you will need to make a generic constraint that specifies something more specific than class
. For example, if you said where Entity : IHasName
and IHasName
defined a property name
, then you would be all set.
Going off Jimmy's post and Zerk's answer:
public static int GetEntityID<T>(string name)
where T : IAnimal
{
T obj = _db.GetTable<T>().SingleOrDefault(t => t.name == name);
return obj != null ? obj.id : 0;
}
Edit2: Since you're new to this, here's an example of the above code usage:
/* You have a LinqToSql object of type Manatee */
int manateeId = MyClass.GetEntity<Manatee>("Jack");
It sounds like you're just trying to implement a repository pattern, which totally makes things easier. Here's an example of how to do it in LinqToSql: http://aspalliance.com/1672_Implementing_the_Repository_Pattern_with_LINQtoSQL.3
Once you learn how to do this, you can modify it to use for NHibernate or ADO.NET
Edit:
public interface IAnimal
{
string name;
}
public Manatee : IAnimal
{
public string name {get;set;}
public int Age {get;set;}
public strin Weight {get;set}
}
I'll guess you're looking for something like:
public static int getEntityID<T>(string name) where T : Entity {
T obj = _db.GetTable<T>().SingleOrDefault(t => t.name == name);
return obj != null ? obj.id : 0;
}
here, Entity would be some base class (or interface)
public abstract class Entity {
public int id { get; set;}
public string name { get; set; }
}
and your types would derive from that.
精彩评论