This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET 3.5+

LINQ Quantifiers

The seventeenth and final part of the LINQ to Objects tutorial looks at the quantifier operators. These methods allow the contents of an entire sequence to be tested, determining if a collection contains a given item or items that satisfy a condition.

Quantifier Standard Query Operators

In the final instalment of the tutorial we will complete the description of the standard query operators provided by Language-Integrated Query (LINQ). The last group of operators are known collectively as the quantifiers. This group contains three related methods, each of which queries the contents of a sequence to determine whether a specific item exists, an item that meets a condition exists, or that every item in the sequence satisfies a predicate.

The quantifier operators do not have equivalent clauses for query expression syntax so no such queries will be included in this article. However, the operators can be used with the sequence produced as the result of a query.

To demonstrate the use of the quantifiers we need some sample data. In this case we will use two arrays and a generic list, as follows:

var primes = new int[] { 2, 3, 5, 7, 11, 13, 17, 19 };
var names = new string[] { "Art", "Bob", "Cath", "Dan", "Ian" };
var empty = new List<int>();


The Contains standard query operator allows you quickly determine if a collection contains a specific value or reference. This extension method does not return an element from the sequence. Instead, it returns a Boolean value of true if the provided item is present and false otherwise. To improve performance, the operator will only enumerate as many of the source sequence's items as is necessary to provide an answer. If the lookup item is found in the collection, any elements following it will not be tested.

The following statements check for the existence of two names in the names array.

bool cath = names.Contains("Cath");     // True
bool jim = names.Contains("Jim");       // False

As with some other standard query operators, the Contains method can be used with a second parameter that provides an alternative comparer. The comparer must implement the IEqualityComparer<T> interface. The following sample shows a case-insensitive lookup.

bool present = names.Contains("cath", StringComparer.OrdinalIgnoreCase); // True


The Any operator has two overloaded versions. The simplest accepts no arguments and returns a Boolean value. If the sequence contains one or more elements, the result is true. If the sequence is empty, the method returns false.

bool anyPrimes = primes.Any();          // True
bool anyEmpty = empty.Any();            // False

The second overload is more useful as it allows you to ask if any of the items in a sequence meet a condition. This version accepts an argument containing a Func delegate, usually expressed as a lambda expression. The expression defines a condition that will be evaluated for each of the elements in the collection. If any of the items in the source collection cause the predicate to return true then the enumeration of the sequence stops and the Any method returns true. If none of the items evaluates as true, the result of the operation is false.

In the following sample code the first line checks for any item in the primes array that has a value larger than ten. The second line checks for values over twenty.

bool over10 = primes.Any(p => p > 10);  // True
bool over20 = primes.Any(p => p > 20);  // False


The All method is similar to the parameterised version of Any. In this case you are able to check that every item in a sequence meets given criteria. The sequence is enumerated until the predicate evaluates as false for an item. If any item fails the condition the result of the operation is false. If every item matches the criteria then the returned value is true.

In the following sample code the first line checks that every item in the primes array has a value lower than twenty. The second line checks that the values are under ten.

bool under20 = primes.All(p => p < 20); // True
bool under10 = primes.All(p => p < 10); // False
30 October 2010