1

我正在使用以下方法:

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.

我该如何解决?

4

2 回答 2

2

Linq to Entities 不支持 Contains 方法。在这种情况下,您必须考虑使用包含内存中对象(Linq-to-Objects)的过滤器逻辑。如果由于性能原因这不是一个可行的选择,我建议您创建一个执行包含的存储过程,然后将其映射到您的实体模型。

以下 url 显示了支持的查询运算符http://msdn.microsoft.com/en-us/library/bb738550.aspx

于 2010-05-19T10:36:24.347 回答
-1

实体框架版本 1 不支持包含。版本 4 可以,但如果升级不可行,您可以通过构建表达式树来复制它。

这里有一篇很好的文章和一些示例代码:http: //blogs.msdn.com/alexj/archive/2009/03/26/tip-8-writing-where-in-style-queries-using-linq-to -entities.aspx

于 2010-05-19T12:41:34.133 回答