LINQ Average

Summary: in this tutorial, you’ll learn how to use the LINQ Average() method to calculate the average of a sequence of numbers.

Introduction to the LINQ Average() method

The Average() method is a LINQ extension method that calculates the average of a sequence of numbers. The syntax of the Average() method is as follows:

public static T Average<TSource> (
   IEnumerable<TSource> source
);Code language: C# (cs)

In this syntax:

  • T is the return type of the average method. It can be float, float?, decimal, decimal?, double, and double?.
  • TSource is the type of element of the source sequence.
  • source is the input sequence.

If the source sequence is empty, the Average() method throws an InvalidOperationException. To avoid it, you can use DefaultIfEmpty() method to provide a default value when the source sequence is empty.

The Average has an overload that accepts a selector function as the second argument:

public static T? Average<TSource> (
   IEnumerable<TSource> source, 
   Func<TSource,int?> selector
);Code language: C# (cs)

The selector is a lambda expression that selects the values for calculating the average.

LINQ Average() method examples

Let’s take some examples of using the LINQ Average() method.

1) Using the LINQ Average() method to calculate the average of a sequence of integers

The following example uses the Average() method to calculate the average sequence of integers:

int[] numbers = { 1, 2, 3, 4, 5, 6 };
double average = numbers.Average();
Console.WriteLine($"Average = {average}");Code language: C# (cs)

Output:

Average = 3Code language: C# (cs)

2) Using the LINQ Average() method with a selector function

The following example demonstrates how to use a selector function to calculate the length of a sequence of strings:

string[] words = { "apple", "banana", "strawberry" };

var averageLength = words.Average(word => word.Length);

Console.WriteLine($"Average length = {averageLength} ");Code language: C# (cs)

Output:

Average length = 7Code language: C# (cs)

3) Using the LINQ Average() method with a nullable type

The Average() method automatically handles null values if the input sequence has nullable types. For example:

decimal?[] numbers = { 1.0M, null, 3.0M };
decimal? average = numbers.Average();
Console.WriteLine($"Average = {average}");Code language: C# (cs)

Output:

Average = 2.0Code language: C# (cs)

4) Using the LINQ Average() method with an empty sequence

The following example demonstrates how to use the Average() method with the DefaultIfEmpty() method:

decimal[] numbers = {};
var average = numbers.DefaultIfEmpty(0).Average();
Console.WriteLine($"Average = {average}");Code language: C# (cs)

Output:

Average = 0Code language: C# (cs)

Summary

  • Use the LINQ Average() method to calculate the average numbers in a sequence.
  • Use the DefaultIfEmpty() method with the Average() method to provide a default value and avoid InvalidOperationException if the input sequence is empty.
Was this tutorial helpful ?