C# Reference ยท LINQ
LINQ Count(), Any() and All() methods
Reference for LINQ Count, LongCount, Any, All and Contains in C#: predicates, empty sequences, TryGetNonEnumeratedCount and performance traps.
Count() returns how many elements a sequence has, or how many satisfy a predicate. Any() answers "is there at least one?", and All() answers "does every element satisfy this condition?". The last two are quantifiers and stop as soon as the answer is known.
Choosing the right one matters for both clarity and speed: Any() is almost always better than Count() > 0, because it does not walk the entire sequence.
int adults = people.Count(p => p.Age >= 18); bool hasErrors = lines.Any(l => l.StartsWith("ERROR")); bool allPositive = nums.All(n => n > 0);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static int Count<TSource>(this IEnumerable<TSource> source); public static int Count<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate); public static long LongCount<TSource>(this IEnumerable<TSource> source); public static bool Any<TSource>(this IEnumerable<TSource> source); public static bool Any<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate); public static bool All<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate); public static bool TryGetNonEnumeratedCount<TSource>(this IEnumerable<TSource> source, out int count);
| Name | Description |
|---|---|
source |
The sequence to inspect. |
predicate |
A test applied to each element. |
count (out) |
The number of elements when it is known without enumerating. |
Returns: Count() an int, LongCount() a long, Any() and All() a bool. TryGetNonEnumeratedCount() returns true only if the size is available cheaply.
Errors: ArgumentNullException for a null source or predicate. Count() throws OverflowException if the result exceeds int.MaxValue (use LongCount()).
Usage notes
- Prefer
Any()overCount() > 0.Count()over a lazy sequence has to run the whole query;Any()stops at the first element (see the second example). - Use the property when you have one.
list.Count,array.Lengthanddictionary.Countare O(1) and need no LINQ.Count()itself checks forICollection<T>and returns the property in that case. All()on an empty sequence istrue;Any(predicate)on an empty sequence isfalse. Remember this when validating that a list is non-empty and valid: combineAny()andAll().All()stops at the first failure, andAny(predicate)at the first success. Side effects in predicates may therefore run only a few times.- Do not call
Count()repeatedly in a loop condition over a lazy query: each call re-runs the query. Materialize withToList()first. - Multiple enumeration:
if (query.Any()) foreach (var x in query)evaluates a deferred query twice. For expensive or non-repeatable sources, enumerate once. - Containment:
Contains()tests for a specific value; see its page. CountBy()is new in .NET 9 and returns key-count pairs.