Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the opposite method of Any<T>

Tags:

c#

linq

How can I check with Linq if a collection does not contain an object. I.E. The opposite of Any<T>.

I could invert the result with a ! but for readability I wondered if there was a more better way to do this? Should I add the extension myself?

like image 212
Caspar Kleijne Avatar asked Jan 05 '12 11:01

Caspar Kleijne


1 Answers

You can easily create a None extension method:

public static bool None<TSource>(this IEnumerable<TSource> source) {     return !source.Any(); }  public static bool None<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) {     return !source.Any(predicate); } 
like image 76
Thomas Levesque Avatar answered Oct 10 '22 04:10

Thomas Levesque