Is there an opposite of the LINQ All method?

I am currently using

a_list.All(item => !(item.field_is_true == true)) 

which works well, but I would like to know if there is a suitable LINQ method to do the opposite.

+8
c # linq
source share
4 answers

All () checks that the given Predicate returns true for all elements. Regarding the development of frameworks, it would be pointless to write a separate method that checks that this Predicate returns false for all elements, since it is so easy to "not" a predicate. However, you can write your own extension method:

 public static bool None<T>(this IEnumerable<T> source, Func<T, bool> predicate) { return !source.Any(predicate); } 
+18
source share

The exact opposite of All() is essentially None , but since LINQ does not have a None() method, you can accomplish the same result via !set.Any() .

 !a_list.Any(item => !(item.matches == true)) 

This will result in true if none of the items in the a_list has matches , which is not true.

This double negative result is a bit confusing, therefore, to provide a simpler example:

 names.All(item => item.StartsWith("R")) 

true if all elements in names start with R (as you already know).

 !names.Any(item => item.StartsWith("R")) 

true if none of the elements in names start with R.

Based on your comment below, it sounds like you're just looking for a way to achieve the same result as your current code snippet, but in a different way. This should provide the same result as the current code, but without ! in the predicate:

 !a_list.Any(item => item.matches == true) 

This can be further simplified:

 !a_list.Any(item => item.matches) 

I would suggest that yours might be simplified, namely:

 a_list.All(item => !item.matches) 

There is rarely a good reason for explicitly comparing a boolean with true or false .

+6
source share

You wrote:

 a_list.All(item => !(item.field_is_true == true)) 

how it's done:

 a_list.All(item => item.flag== false) // more readable to me... //return true if all of the items have a **flase** value on their flag 

you can also use .any () to achieve the same result:

 !a_list.Any(item => item.flag==true) 

as for performance problems: .any () vs .all () - both will have identical performance (when linq is used for an object), find here: LINQ: not any vs All Do not

+2
source share

Instead of denying the condition All (), just use Any () with the same predicate and handle the return boolean accordingly.

So, instead of:

 bool conditionDoesntExist = a_list.All(item => !(item.field_is_true == true)); 

you may have

 bool conditionDoesExist = a_list.Any(item => item.field_is_true == true) 

Note the change in flag name. (Of course, I skip semantic things, such as the original predicate, can be written as item => item.field_is_true == false or just item => !item.field_is_true ).

If you want to keep the flag name the same, but use Any () anyway, but deny it:

 bool conditionDoesntExist = !a_list.Any(item => item.field_is_true == true); 
+1
source share

All Articles