如何创建通用 lambda 表达式。我的许多实体执行相同的 Lambda 表达式谓词
我使用实体框架 4。
如何执行通用Where Lambda 子句。?
我有许多需要相同Where 查询的实体。
public Func<SupplierTypeText, bool> GetLmbLang()
{
return (p => p.LangID == 1);
}
public Func<ProductText, bool> GetLmbLang()
{
return (p => p.LangID == 1);
}
public Func<CategoryText, bool> GetLmbLang()
{
return (p => p.LangID == 1);
}
我想要一个通用方法,比如
//public interface IRepository<T> : IRepository<T> where T : class
public Func<T, bool> GenericGetLmbLang()
{
return (p => p.LangID == 1);
}
目前,我硬编码语言 ID == 1,它将来自用户会话以使其动态。
如果我可以直接在Where子句中直接调用GetLmbLang(),那将非常有用。
var ViewModel = _db.Suppliers.Select(model => new
{
model,
SupType = _db.SupplierTypeTexts.Where(a => GenericGetLmbLang())
});
-----更新--------
这是我尝试的方法,但没有任何效果。
我的基类
public class BaseGenericModel
{
public int LangID { get; set; }
public Func<BaseGenericModel, bool> GetLmbLang()
{
return (p => p.LangID == 1);
}
}
我的接口是
public interface IBaseRepository<T> where T : BaseGenericModel
{
Func<T, bool> GetLmbLang();
}
public class BaseRepository<T> : IBaseRepository<T> where T : BaseGenericModel
{
public Func<T, bool> GetLmbLang()
{
return (p => p.LangID == 1);
}
}
我无法从我的SupplierTypeText、ProductText、CategoryText 调用此存储库。那是行不通的。
I use Entity Framework 4.
How can I perform a Generic Where Lambda Clause.?
I Have many Entity that need the same Where Query.
public Func<SupplierTypeText, bool> GetLmbLang()
{
return (p => p.LangID == 1);
}
public Func<ProductText, bool> GetLmbLang()
{
return (p => p.LangID == 1);
}
public Func<CategoryText, bool> GetLmbLang()
{
return (p => p.LangID == 1);
}
I would like to have a generic method like
//public interface IRepository<T> : IRepository<T> where T : class
public Func<T, bool> GenericGetLmbLang()
{
return (p => p.LangID == 1);
}
For the moment, I hardcoded Language ID == 1, that will be from the user session to make it dynamic.
That's would be very usefull if I Can directly call the GetLmbLang() Directly in the Where clause.
var ViewModel = _db.Suppliers.Select(model => new
{
model,
SupType = _db.SupplierTypeTexts.Where(a => GenericGetLmbLang())
});
------UPDATE--------
Here is what I Trying and nothing works.
My Base Class
public class BaseGenericModel
{
public int LangID { get; set; }
public Func<BaseGenericModel, bool> GetLmbLang()
{
return (p => p.LangID == 1);
}
}
My interface is
public interface IBaseRepository<T> where T : BaseGenericModel
{
Func<T, bool> GetLmbLang();
}
public class BaseRepository<T> : IBaseRepository<T> where T : BaseGenericModel
{
public Func<T, bool> GetLmbLang()
{
return (p => p.LangID == 1);
}
}
I Can't call this repository form my SupplierTypeText,ProductText,CategoryText. That's doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎拥有三种不同的类型,它们都具有 LangID 属性。我会让它们从定义此属性的公共基类派生:
然后有一个方法:
也可以使用包含此属性的接口。
如果你想让它通用,你可以,但如果你想使用
LangID
属性,你仍然需要一个通用约束来指示通用基类型:It seems that you have three different types all having the LangID property. I would make them derive from a common base class where this property is defined:
and then have a single method:
An interface containing this property could also be used.
If you want to make it generic you could but you will still need a generic constraint to indicate a common base type if you want to use the
LangID
property: