With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

Quantifiers

Method

Description

SQL equivalents

`Contains`

Returns `true` if the input sequence contains the given element

`WHERE…IN (…)`

`Any`

Returns `true` if any elements satisfy the given predicate

`WHERE…IN (…)`

`All`

Returns `true` if all elements satisfy the given predicate

`WHERE (…)`

`SequenceEqual`

Returns `true` if the second sequence has identical elements to the input sequence

Contains and Any

The `Contains` method accepts an argument of type `TSource; Any` accepts an optional predicate.

`Contains` returns `true` if the given element is present:

`	bool isTrue = new int[] { 2, 3, 4}.Contains (3);`

`Any` returns `true` if the given expression is true for at least one element. We can rewrite the preceding query with `Any` as follows:

`	bool isTrue = new int[] { 2, 3, 4 }.Any (n => n == 3);`

`Any` can do everything that `Contains` can do, and more:

`	bool isFalse = new int[] { 2, 3, 4 }.Any (n => n > 10);`

Calling `Any` without a predicate returns `true` if the sequence has one or more elements. Here’s another way to write the preceding query:

```	bool isFalse = new int[] { 2, 3, 4 }
.Where (n => n > 10).Any();```

`Any` is particularly useful in subqueries.

All and SequenceEqual

`All` returns `true` if all elements satisfy a predicate. The following returns customers whose purchases are less than \$100:

```	dataContext.Customers.Where
(c => c.Purchases.All (p => p.Price < 100));```

`SequenceEqual` compares two sequences. To return `true`, each sequence must have identical elements, in the identical order.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required