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

Aggregation Methods

Returns the number of elements in the input sequence, optionally
satisfying a predicate

COUNT( )

Min, Max

Returns the smallest or largest element in the
sequence

MIN( ), MAX( )

Sum, Average

Calculates a numeric sum or average over elements in the
sequence

SUM( ), AVG ()

Aggregate

Performs a custom aggregation

Exception thrown

Count and LongCount

Argument

Type

Source sequence

IEnumerable<TSource>

Predicate (optional)

TSource =>
bool

Count simply enumerates over
a sequence, returning the number of items:

int fullCount = new int[] { 5, 6, 7}.Count(); // 3

The internal implementation of Enumerable.Count tests the input sequence to
see whether it happens to implement ICollection<T>. If it does, it simply
calls ICollection<T>.Count.
Otherwise, it enumerates over every item, incrementing a
counter.

You can optionally supply a predicate:

int digitCount =
"pa55w0rd".Count (c => char.IsDigit (c)); // 3

LongCount does the same job
as Count, but returns a 64-bit
integer, allowing for sequences of greater than 2 billion
elements.