Users Pricing

articles

home / developersection / articles / explanation of all linq aggregate functions
Explanation of all LINQ Aggregate functions

Explanation of all LINQ Aggregate functions

Ashutosh Patel 2838 12 Feb 2025 Updated 12 Feb 2025

In LINQ (Language-Integrated Query), aggregate functions such as SUM, MIN, MAX, COUNT, AVERAGE and AGGREGATE help perform calculations on collections. Let's take a look at each with C# examples.
1. SUM()
It calculates the total sum of numeric elements in a collection.

List<int> NumberList = new List<int> { 2, 5, 7, 3, 8, 4, 9, 4, 6, };
int sum = NumberList.Sum(); 
Console.WriteLine("Sum: " + sum); // Output: Sum: 48

2. MIN()

It returns the smallest number in the collection.

List<int> NumberList = new List<int> { 2, 5, 7, 3, 8, 4, 9, 4, 6, };
int min = NumberList.MIN(); 
Console.WriteLine("Min: " + min); // Output: Min: 2

3. MAX()

It returns the largest number in the collection.

int max = NumberList.Max();
Console.WriteLine("Max: " + max); // Output: Max: 9

4. COUNT()

It returns the number of elements in the collection.

int count = NumberList.Count();
Console.WriteLine("Count: " + count); // Output: Count: 9

5. AVERAGE()

It computes the mean (average) of numeric elements.

int avg = (int)NumberList.Average();
Console.WriteLine("Average: " + avg); // Output: Average: 5

6. AGGREGATE()

It performs custom aggregation by applying a function cumulatively.

int aggValue = NumberList.Aggregate((a, b) => a + b); // ((((2+5)+7)+3)+8)...
Console.WriteLine("Aggregate: " + aggValue); // Output: Aggregate: 48

Complete Example in C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace MyConsoleApplication
{
   class Program
   {
       List<int> NumberList = new List<int> { 2, 5, 7, 3, 8, 4, 9, 4, 6, };
       static void Main(string[] args)
       {
           Program prog = new Program();
           Console.WriteLine("Sum of Value Is: {0}", prog.SumValue());
           Console.WriteLine("Max Value Is: {0}", prog.MaxValue());
           Console.WriteLine("Min Value Is: {0}", prog.MinValue());
           Console.WriteLine("Average Values Is: {0}", prog.AvgValue());
           Console.WriteLine("Count of Values Is: {0}", prog.CountValue());
           Console.WriteLine("Aggragate of Value is: {0}", prog.AggrateValue());
           Console.WriteLine("Press any key to exit");
           Console.ReadLine();
       }
       public int SumValue()
       {
           int sumNum = NumberList.Sum();
           return sumNum;
       }
       public int MaxValue()
       {
           int maxNum = NumberList.Max();
           return maxNum;
       }
       public int MinValue()
       {
           int minNum = NumberList.Min();
           return minNum;
       }
       public int AvgValue()
       {
           int avgvalue = (int)NumberList.Average();
           return avgvalue;
       }
       public int CountValue()
       {
           int countvalue =  NumberList.Count();
           return countvalue;
       }
       public int AggrateValue()
       {
           int aggValue = NumberList.Aggregate((a, b) => a + b); // ((((2+5)+7)+3)+8)...
           return aggValue;
       }
   }
}

I hope you understand clearly. Thanks!

Also, Read: What is LINQ


Ashutosh Patel

Web Developer

I am a professional .NET developer with over 4 years of hands-on industry experience in designing, developing, and maintaining scalable web applications. I specialize in .NET Core, C#, RESTful APIs, and database-driven systems using SQL Server.