LINQ 和 OR 标准

发布于 2024-12-01 07:00:27 字数 314 浏览 1 评论 0原文

如何以编程方式创建 EF 查询(带有 lambda 的扩展方法)。我了解标准。这是伪代码:

var query = repository.Where(x => x.Name == "aName");

foreach(string filter in filters)
{
   query = query.Where(x => x.FilterValue.Contains(filter))
}

但我想要的不是 and 运算符。我想要一个 or 运算符。我该怎么做?如何在代码中创建复杂的条件树?

how can I programmatically create an EF query (extension methods with lambda). I understand the and criteria. Here is the pseudo-code:

var query = repository.Where(x => x.Name == "aName");

foreach(string filter in filters)
{
   query = query.Where(x => x.FilterValue.Contains(filter))
}

But what I want is not an and operator. I would like an or operator. How do I do this? How can I create complex criteria Trees in code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

小傻瓜 2024-12-08 07:00:27

http://www.albahari.com/nutshell/predicatebuilder.aspx

PrdicateBuilder 是一个不错的解决方案。但它很复杂,不太容易理解。请参阅评论中其他问题的链接。

还发现这很有用:有时从另一个方向来很方便:

string[] filter = {"A", "B"};
var returnValue = repository
                .Where(x => x.Name == "aName")
                .Where(x => filter.Any(f => (x.FilterValue).Contains(f)))
                .ToList();

http://www.albahari.com/nutshell/predicatebuilder.aspx

PrdicateBuilder is a nice solution. But it is complicated and not so easy to understand. See Link to other question in comment.

Also found this useful: Sometimes it is convenient to come from the other direction:

string[] filter = {"A", "B"};
var returnValue = repository
                .Where(x => x.Name == "aName")
                .Where(x => filter.Any(f => (x.FilterValue).Contains(f)))
                .ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文