如何在“或”上拆分或连接 linq 查询(不使用联盟)?

发布于 2024-12-10 11:04:31 字数 752 浏览 1 评论 0原文

假设您正在搜索具有两个布尔属性 A 和 B 的某个对象。

如果您有两个 linq 查询怎么办:

IQueryable<ObjectType> query = getIQueryableSomehow()
query.Where(x => x.A);

IQueryable<ObjectType> query2 = getIQueryableSomehow()
query2.Where(x => x.B);

如何将这些查询连接在一起,以便它们与此等效?:

IQueryable<ObjectType> query3 = getIQueryableSomehow()
query3.Where(x => x.A || x.B)

我想使用 query3 = query.Union (query2),但遗憾的是我的 Linq 提供者联合不受支持。

我将 x => 的情况分开xA&& xB 通过链接 where 子句。这就是我的意思:

IQueryable<ObjectType> query = getIQueryableSomehow();
query = query.Where(x => x.A);
query = query.Where(x => x.B);

对于 or 情况是否有类似的解决方法?

谢谢,

艾萨克

Suppose you're searching some object with two boolean properties, A and B.

What if you have two linq queries:

IQueryable<ObjectType> query = getIQueryableSomehow()
query.Where(x => x.A);

IQueryable<ObjectType> query2 = getIQueryableSomehow()
query2.Where(x => x.B);

How can I join these queries together so that they are equivalent to this?:

IQueryable<ObjectType> query3 = getIQueryableSomehow()
query3.Where(x => x.A || x.B)

I would like to use query3 = query.Union(query2), but sadly in my Linq provider union is not supported.

I split up the case for x => x.A && x.B by chaining the where clause. This is what I mean:

IQueryable<ObjectType> query = getIQueryableSomehow();
query = query.Where(x => x.A);
query = query.Where(x => x.B);

Is there some similar workaround for the or case?

Thanks,

Isaac

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

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

发布评论

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

评论(2

腹黑女流氓 2024-12-17 11:04:32

如果您的 LINQ 提供程序不支持 Union 运算符,您始终可以首先使用 ToArray() 具体化查询结果,然后使用 LINQ-to-objects 提供程序来执行它支持的并集,如下所示

IQueryable<ObjectType> query = getIQueryableSomehow() 
query.Where(x => x.A); 

IQueryable<ObjectType> query2 = getIQueryableSomehow() 
query2.Where(x => x.B); 

IQueryable<ObjectType> query3 = query.ToArray().AsQueryable<ObjectType>().Union(query2);

:只要你联合的结果不是太大。

If your LINQ provider doesn't support the Union operator, you can always first materialize the query results using ToArray(), and then use the LINQ-to-objects provider to perform the union, which it supports, like so:

IQueryable<ObjectType> query = getIQueryableSomehow() 
query.Where(x => x.A); 

IQueryable<ObjectType> query2 = getIQueryableSomehow() 
query2.Where(x => x.B); 

IQueryable<ObjectType> query3 = query.ToArray().AsQueryable<ObjectType>().Union(query2);

This will work well as long as your results being unioned are not too large.

痴情 2024-12-17 11:04:31

使用 谓词生成器

IQueryable<ObjectType> query = getIQueryableSomehow();
var predicate = PredicateBuilder.False<ObjectType>();
predicate = predicate.Or(x => x.A);
predicate = predicate.Or(x => x.B);

var list = query.Where(predicate).ToList();

using Predicate Builder

IQueryable<ObjectType> query = getIQueryableSomehow();
var predicate = PredicateBuilder.False<ObjectType>();
predicate = predicate.Or(x => x.A);
predicate = predicate.Or(x => x.B);

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