Linq 和 Contains()

发布于 2024-09-02 19:54:40 字数 764 浏览 4 评论 0原文

我正在使用以下方法:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

比忠 2024-09-09 19:54:40

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

牵强ㄟ 2024-09-09 19:54:40

实体框架版本 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文