Aggregate Operators in Linq

by Sachin Singh


Posted on Saturday, 06 February 2021

Tags: Aggregate Operators in Linq

Sometimes we need to perform some mathematical operations on a particular numeric column of a database table or items of in-memory collections like List or Array etc, for example, if you have to calculate the total profit in a month after selling some items, then you will need to calculate the sum of prices of the total sold items, achieving this with plain C# code is a little bit noisy but LINQ provides us some built-in mathematical operators which can be used to achieve such requirements easily.

Suppose, you have a list of integers and you have to find the average of items, then with plain c# your code will look something like shown below.


  List<int> numbers= new List<int>{10,2,3,40,78};
  int sum=0,i=0;
  foreach(var item in numbers)
  {
  i++;
  sum=sum+item;
  }
  var average= sum/i;
 Console.WriteLine(average);
 

As you can see in the above example, first, we are calculating the sum of all items and the total number of items in the List and finally we are dividing the sum by several elements to calculate the average, but Linq provides us an Average operator that does all the calculation behind the scene for us and returns the average and thus we can easily calculate average with just one line of code as shown below.


 List<int> numbers= new List<int>{10,2,3,40,78};
 var Average= numbers.Average();
 Console.WriteLine(Average);
 

So, we can conclude that the aggregate operators are used to perform mathematical operations like Average, Aggregate, Count, Max, Min and Sum, on the numeric property of the objects or elements in the collection. Following diagram explains aggregate operators in more detail.

Linq Aggregate Operators
Aggregate Operators in LINQ

Aggregate Operator

Aggregate Operators are used to perform custom aggregation operations over a collection of items.

List<string> stringList= new List<string>{"One","Two","Three","Four","Five"};
var commaSeparatedList= stringList.Aggregate((s1,s2)=>S1+","+s2);
Console.WriteLine(commaSeparatedList);

Average

Average Operator is used to find out the average of numeric items in the collection.


 List<int> numbers= new List<int>{10,2,3,40,78};
 var Average= numbers.Average();
 Console.WriteLine(Average);
 var EvenAverage= numbers.Where(x=>x%2==0).Average();
 Console.WriteLine(EvenAverage);

Count

Count is used to count the number of elements in the collection


 List<int> numbers= new List<int>{10,2,3,40,78};
  var count= numbers.Count();
  Console.WriteLine(count);
  var EvenCount= numbers.Count(x=>x%2==0);
  Console.WriteLine(EvenCount);
 

Max

Max is used to find out the largest value in the collection.


 List<int> numbers= new List<int>{10,2,3,40,78};
 var largest= numbers.Max();
 Console.WriteLine(largest);
 var EvenMax= numbers.Max(x=>x%2==0);
 Console.WriteLine(EvenMax);
 

Min

Min is used to find out the smallest value in the collection.


 List<int> numbers= new List<int>{10,2,3,40,78};
 var smallest= numbers.Min();
 Console.WriteLine(smallest);
 var EvenSmallest= numbers.Min(x=>x%2==0);
 Console.WriteLine(EvenSmallest);
 

Sum

Sum is used to find out the sum of values in the collection


 List<int> numbers= new List<int>{10,2,3,40,78};
 var total= numbers.Sum();
 Console.WriteLine(total);
 var EvenTotal= numbers.Where(x=>x%2==0).Sum();
 Console.WriteLine(EvenTotal);
 

We will discuss each of these aggregate operators in the coming chapters in detail.