15

I'm currently using

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

which works well, but I'd like to know if there was a proper LINQ method to do the opposite of all.

TankorSmash
  • 12,186
  • 6
  • 68
  • 106

5 Answers5

27

All() checks that a given Predicate returns true for all items. In terms of framework development, it wouldn't make any sense to write a seperate method that checks that a given Predicate returns false for all items, as 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);
}
Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
7

The exact opposite of All() is essentially None, but since LINQ has no None() method, you can accomplish the same result through !set.Any().

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

This will produce true if none of the items in a_list have a matches value that is true.

Another example:

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

is true if all of the items in names start with R (as you know already).

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

is true if none of the items in names start with R.

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

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

This can be further simplified to:

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

I'd imagine yours could be simplified as well, to this:

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

There's rarely a good reason to explicitly compare a boolean value with true or false.

JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • 1
    Assuming `.matches` is always `true`, wouldn't you only want the second `!`, without the first? – TankorSmash Jan 20 '13 at 06:29
  • 1
    @TankorSmash I'm not sure. You haven't fully explained what you're trying to do. I gave you the exact opposite of your example. What are you trying to determine, specifically? – JLRishe Jan 20 '13 at 06:32
2

you wrote:

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

that is like doing:

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 achieves the same result:

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

as for performence issues: .any() vs .all() - both would have identical performance (when linq to object is used) , find more here : LINQ: Not Any vs All Don't

Community
  • 1
  • 1
yoav barnea
  • 5,836
  • 2
  • 22
  • 27
1

Rather than negate the All() condition, simply use the Any() with the same predicate and treat the returned boolean appropriately.

So, rather than:

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

you can have

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

Note the change in name of the flag. (Of course I'm overlooking semantic stuff like the original predicate could have been written as item => item.field_is_true == false or simply item => !item.field_is_true ).

If you want to keep the flag name the same then still use the Any() but negate it:

bool conditionDoesntExist = !a_list.Any(item => item.field_is_true == true);
slugster
  • 49,403
  • 14
  • 95
  • 145
  • 1
    there are not supposed to be performence differences when using **linq to object**. see [link](http://stackoverflow.com/a/9027666/1219182) – yoav barnea Jan 20 '13 at 08:24
0

All

a_list.All(item => item.condition)

Some

a_list.Any(item => item.condition)

Not all

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

or:

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

None

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

or

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