相关疑难解决方法(0)

通过Generic类型参数访问属性

我正在尝试为我的模型创建一个通用存储库.目前我有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)

c# generics wpf linq-to-sql

5
推荐指数
1
解决办法
6165
查看次数

标签 统计

c# ×1

generics ×1

linq-to-sql ×1

wpf ×1