[英]Condensing multiple ORs in a Linq Where Clause
Let's say I have this code: 假设我有以下代码:
myList = myList.Where(x => x[0] > .0005 || x[1] > .0005 || x[2] > .0005).ToList();
There has to be a way to just do this over an enumerable range? 必须有一种方法可以在无数范围内做到这一点?
I was thinking something along the lines of 我在想一些类似的事情
myList = myList.EnumerableRange(0, x.Count).Where(i => x[i] > .0005)
But this doesn't seem to be working. 但这似乎不起作用。 What am I missing/Is there a better way? 我缺少什么/有更好的方法吗?
You can use the LINQ method Any
for that. 您可以为此使用LINQ方法Any
。
myList = myList.Where(x => x.Any(d => d > .0005)).ToList();
I am assuming you are looking to check all the items in x
. 我假设您要检查x
所有项目。 Otherwise you will have to use the LINQ extensions Take
and Skip
to get a slice of the array. 否则,您将不得不使用LINQ扩展Take
and Skip
来获取阵列的一部分。 Also assuming that x
is an IEnumerable<T>
. 还要假设x
是IEnumerable<T>
。 Note that, if you expect x
to be null
you will have to handle it 请注意,如果您希望x
为null
,则必须处理它
您仍然需要执行-或等效操作-将多个Where
子句有效地与在一起:
yList = myList.Where(x => Enumerable.range(0, x.Count).Any(i => x[i] > .0005)).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.