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.
int total = nums.Sum(); double mean = nums.Average(); var oldest = people.MaxBy(p => p.Age);
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 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-typeMin()/Max()throw. Reference types and nullable types returnnull. Guard withAny()orDefaultIfEmpty(). - Nullable sums and averages ignore nulls.
Average()of{4, null, 8}is 6, not 4. An all-null source givesnullfor averages and 0 for sums. - Overflow: sum into
longordecimalby casting inside the selector when values can be large. - Floating point:
doublesums depend on addition order and give the usual rounding error (0.1 + 0.2 + 0.3is not exactly0.6). Usedecimalfor money. MaxBy()returns the first element that has the maximum key;Max()returns the key value.MinBy()andMaxBy()do not throw on empty reference-type sequences; they returnnull.- Complexity: O(n), one pass, no extra memory.