articles

home / developersection / articles / explanation of all linq aggregate functions

Explanation of all LINQ Aggregate functions

Explanation of all LINQ Aggregate functions

Ashutosh Kumar Verma 505 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


linq c#  linq 
Updated 12-Feb-2025

Hi! This is Ashutosh Kumar Verma. I am a software developer at MindStick Software Pvt Ltd since 2021. I have added some new and interesting features to the MindStick website like a story section, audio section, and merge profile feature on MindStick subdomains, etc. I love coding and I have good knowledge of SQL Database.

Leave Comment

Comments

Liked By