I've been running into situations where I feel I'm lacking a LINQ extension method which effectivelly checks if there is no match of the specified predicate in a collection. There is Any
and All
, but if I for instance use the following code:
if (Objects.All(u => u.Distance <= 0))
This returns true if all the objects in the collection are 0 or less yards away.
if (Objects.Any(u => u.Distance <= 0))
This returns true if there is at least one object in the collection which is 0 or less yards away from me.
So far so good, both those methods make sense and the syntax for them makes sense too. Now, if I want to check if there is no object with 0 or less distance, I'd have to invert the predicate inside the All
method to >= 0
instead of <= 0
or call !All()
, which in some cases results in very poorly readable code.
Is there no method which effectively does Collection.None(u => u.Distance <= 0)
to check if there is no object in the collection which is 0 or less yards away? It's syntactic sugar more than an actual problem, but I just have the feeling it's missing.
Any
). The OP doesn't like it, not because it doesn't do what the OP's looking for, but because it doesn't look pretty. What's pretty and what isn't is largely opinion based. I didn't vote, nor am I going to, but I can see why someone else might.Any
, I loveAny
. I'm asking if there's a method in existence in LINQ which I feel is missing:None
.