Linq 和 Contains()
我正在使用以下方法:
public PageOfList<ConsaltQuestion> Filter(int? type, int pageId, EntityCollection<ConsaltCost> ConsaltRoles)
{
// return _dataContext.ConsaltQuestion.Where((o => o.Type == type || type == null) && (o=>o.Paid == paid));
return (from i in _dataContext.ConsaltQuestion where ((i.Type == type || type == null) && (i.Paid == true) && (ConsaltRoles.Contains(ConsaltCostDetails(i.Type.Value)))) select i).ToList().ToPageOfList(pageId, 20);
}
它返回错误:
LINQ to Entities does not recognize the method 'Boolean Contains(mrhome.Models.ConsaltCost)' method, and this method cannot be translated into a store expression.
我该如何修复它?
I am using following method:
public PageOfList<ConsaltQuestion> Filter(int? type, int pageId, EntityCollection<ConsaltCost> ConsaltRoles)
{
// return _dataContext.ConsaltQuestion.Where((o => o.Type == type || type == null) && (o=>o.Paid == paid));
return (from i in _dataContext.ConsaltQuestion where ((i.Type == type || type == null) && (i.Paid == true) && (ConsaltRoles.Contains(ConsaltCostDetails(i.Type.Value)))) select i).ToList().ToPageOfList(pageId, 20);
}
it returns the error:
LINQ to Entities does not recognize the method 'Boolean Contains(mrhome.Models.ConsaltCost)' method, and this method cannot be translated into a store expression.
How can I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Linq to Entities 不支持 Contains 方法。在这种情况下,您必须考虑使用内存中对象 (Linq-to-Objects) 的 Contains 过滤器逻辑。如果由于性能原因这不是一个可行的选项,我建议您创建一个执行包含的存储过程,然后将其映射到您的实体模型。
以下 URL 显示支持的查询运算符 http://msdn.microsoft.com/ en-us/library/bb738550.aspx
Linq to Entities doesn't support the Contains method. In this case you must consider use the Contains filter logic using the in-memory objects (Linq-to-Objects). If it is not a practicable option due to performance reasons, I suggest you to create a stored procedure that performs the contains and then map it to your entity model.
The following url shows the supported query operators http://msdn.microsoft.com/en-us/library/bb738550.aspx
实体框架版本 1 不支持 Contains。版本 4 可以,但如果升级不可行,您可以通过构建表达式树来复制它。
这里有一篇很好的文章,其中包含一些示例代码: http://blogs.msdn.com/alexj/archive/2009/03/26/tip-8-writing-where-in-style-queries -使用-linq-to-entities.aspx
Version 1 of the Entity Framework doesn't support Contains. Version 4 does, but if upgrading isn't feasible you can duplicate it by building up expression tree.
There is a good article here with some example code: http://blogs.msdn.com/alexj/archive/2009/03/26/tip-8-writing-where-in-style-queries-using-linq-to-entities.aspx