What are aggregate functions in LINQ?

LINQ (Language Integrated Query) is divided into two parts. "Language Integrated" means that LINQ is part of the programming language, i.e., C# and VB syntax; these can be shipped with .NET and have LINQ capabilities. "Query" explains that LINQ is used for querying data. The data can be of different types. In short, LINQ is a programming language that is used to query data.

LINQ has multiple functions for multiple uses. In this Answer, we will be discussing the aggregate functions.

What are aggregate functions in LINQ?

Aggregate functions are functions that always return a single value regardless of the number of inputs provided. They are usually used to perform various operations on numeric values, such as summing the numeric values or finding the average of the numeric values. LINQ provides us with the following aggregate functions:

  • Sum(): This function returns the sum of all the numbers provided as input.

  • Max(): This function returns the maximum of all the numbers provided as input.

  • Min(): This function returns the minimum of all the numbers provided as input.

  • Count(): This function returns the total count of all the numbers provided as input.

  • Average(): This function returns the average of the numbers provided as input.

  • Aggregate(): This function returns the accumulated aggregate of all the numbers provided as input.

Syntax

Let's go through the syntax of each aggregate function provided by LINQ.

The Sum() function

int sum = number_list.Sum();
Syntax for the Sum() aggregate function

The Max() function

int max = number_list.Max();
Syntax for the Max() aggregate function

The Min() function

int min = number_list.Min();
Syntax for the Min() aggregate function

The Count() function

int count = number_list.Count();
Syntax for the Count() aggregate function

The Average() function

double average = number_list.Average();
Syntax for the Average() aggregate function

The Aggregate() function

int product = strings_list.Aggregate((acc, x) => acc * x);
Syntax for the Aggregate() aggregate function

Note: In each of the syntax above, number_list is the list of numbers passed as an input to the aggregate functions.

Parameters

Each of the aggregate function takes in a list of numbers as an input. In the Aggregate() method, acc is an accumulator function and x is each element of the list.

Coding example

Let's look at an example where we utilize each of the aggregate function provided by LINQ.

using System;
using System.Linq;
namespace LINQ_methods
{
class Methods
{
static void Main(string[] args)
{
int[] list_of_numbers = new int[] { 10,20,25,49,78,90 };
int sum_total = list_of_numbers.Sum();
Console.WriteLine("Total sum of all numbers = " + sum_total);
int max_number = list_of_numbers.Max();
Console.WriteLine("Largest value of all the numbers = " + max_number);
int min_number = list_of_numbers.Min();
Console.WriteLine("Smallest value of all the numbers = " + min_number);
int count_number = list_of_numbers.Count();
Console.WriteLine("Total number of elements in the list = " + count_number);
var average_number = list_of_numbers.Average();
Console.WriteLine("Average of all the numbers = " + average_number);
string[] list_of_languages = { "Python", "C#", "C++", "Java" };
string result_aggregate = list_of_languages.Aggregate((n1, n2) => n1 + ", " + n2);
Console.WriteLine("Aggregated list of strings = " + result_aggregate);
Console.ReadKey();
}
}
}

Code explanation

  • Line 9: We define the list of numbers.

  • Line 11: We use the Sum() method to find the sum of all the numbers in the defined list.

  • Line 12: We print the statement and total sum using Console.WriteLine().

  • Line 14: We use the Max() method to find the largest value of all the numbers in the defined list.

  • Line 15: We print the statement and largest value using Console.WriteLine().

  • Line 17: We use the Min() method to find the smallest value of all the numbers in the defined list.

  • Line 18: We print the statement and smallest value using Console.WriteLine().

  • Line 20: We use the Count() method to find the total number of all the elements in the defined list.

  • Line 21: We print the statement and count using Console.WriteLine().

  • Line 23: We use the Average() method to find the average of all the numbers in the defined list.

  • Line 24: We print the statement and average value using Console.WriteLine().

  • Line 26: We define a string list of programming languages.

  • Line 27: We use the Aggregate() method to find the accumulated aggregate. The n1 will accumulate each element present in the list.

  • Line 28: We print the statement and aggregate value using Console.WriteLine().

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved