Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท LINQ

LINQ Sum(), Average(), Min() and Max() methods

Reference for LINQ Sum, Average, Min, Max, MinBy and MaxBy in C#: empty sequences, nullable values, overflow, selectors and floating-point notes.

Sum(), Average(), Min() and Max() reduce a sequence of numbers (or of any comparable values, for Min() and Max()) to one value. Each has an overload that takes a selector, so you can write orders.Sum(o => o.Total) without a separate Select().

MinBy() and MaxBy() (.NET 6+) return the element with the smallest or largest key instead of the key itself. All of these run immediately and read the whole sequence once.

C#
int total = nums.Sum();
double mean = nums.Average();
var oldest = people.MaxBy(p => p.Age);

Try it

C# The four aggregates

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.

C# Empty sequences, nulls and overflow

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

C#
public static int Sum(this IEnumerable<int> source);
public static decimal Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector);
public static double Average(this IEnumerable<int> source);
public static TSource? Min<TSource>(this IEnumerable<TSource> source);
public static TResult? Max<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);
public static TSource? MinBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);
public static TSource? MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);

Sum() and Average() exist for int, long, float, double, decimal and their nullable forms, each with a selector overload.

Name Description
source The sequence to reduce.
selector / keySelector Extracts the number or key from each element.

Returns: Sum() has the same type as the elements (int sums stay int); Average() returns double for integers and float, double for those types, and decimal for decimals.

Errors: Sum() on int or long throws OverflowException (checked arithmetic). Average(), and Min()/Max() over value types, throw InvalidOperationException ("Sequence contains no elements") when the sequence is empty. ArgumentNullException for a null source.

Usage notes

  • Empty sequences: Sum() returns 0; Average() and value-type Min()/Max() throw. Reference types and nullable types return null. Guard with Any() or DefaultIfEmpty().
  • Nullable sums and averages ignore nulls. Average() of {4, null, 8} is 6, not 4. An all-null source gives null for averages and 0 for sums.
  • Overflow: sum into long or decimal by casting inside the selector when values can be large.
  • Floating point: double sums depend on addition order and give the usual rounding error (0.1 + 0.2 + 0.3 is not exactly 0.6). Use decimal for money.
  • MaxBy() returns the first element that has the maximum key; Max() returns the key value. MinBy() and MaxBy() do not throw on empty reference-type sequences; they return null.
  • Complexity: O(n), one pass, no extra memory.