如何在“或”上拆分或连接 linq 查询(不使用联盟)?
假设您正在搜索具有两个布尔属性 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的 LINQ 提供程序不支持 Union 运算符,您始终可以首先使用 ToArray() 具体化查询结果,然后使用 LINQ-to-objects 提供程序来执行它支持的并集,如下所示
:只要你联合的结果不是太大。
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:
This will work well as long as your results being unioned are not too large.
使用 谓词生成器
using Predicate Builder