Predicates

In the world of LINQ, predicate operators express Boolean-valued truths about sequences and their elements. Operators such as Any and All should sound familiar to those who’ve learned basics of predicate logic. Other operators include SequenceEqual (to compare two sequences) and Contains (to ask membership questions about elements in sequences). Let’s take a look at all of those.

All and Any

All and Any evaluate a predicate for each of the elements in a given sequence. If all the elements pass the predicate, All will return true. If some of the elements pass the predicate, Any will also return true:

static bool All<T>(this IEnumerable<T> source, Func<T, bool> predicate);static bool Any<T>(this IEnumerable<T> source);static bool Any<T>(this ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.