我正在尝试为我的模型创建一个通用存储库.目前我有3种不同的型号,它们之间没有任何关系.(联系人,备注,提醒).
class Repository<T> where T:class
{
public IQueryable<T> SearchExact(string keyword)
{
//Is there a way i can make the below line generic
//return db.ContactModels.Where(i => i.Name == keyword)
//I also tried db.GetTable<T>().Where(i => i.Name == keyword)
//But the variable i doesn't have the Name property since it would know it only in the runtime
//db also has a method ITable GetTable(Type modelType) but don't think if that would help me
}
}
Run Code Online (Sandbox Code Playgroud)
在MainViewModel中,我像这样调用Search方法:
Repository<ContactModel> _contactRepository = new Repository<ContactModel>();
public void …Run Code Online (Sandbox Code Playgroud)