Aggregate functions are applied to each element in a sequence and finally returns a single aggregated value.For example *sum()* function is applied to the first,second element to calculate their sum.This sum is then added to the third element to calculate the sum of first three elements.Finally the total sum of all the elements is calculated and returned.

Some of the aggregate operators are:

- Sum
- Count
- Average
- Min
- Max

** Sum ** Sum operator is applied to the values in a sequence and calculates a single value which is the sum of all the values in a collection.

In the following example we have a collection of students.Each element in the collection is represented by an object of Student class.

1 2 3 4 5 6 | class Student { public String Name { get; set; } public int TotalMarks { get; set; } //other properties } |

The students are added in the collection:

1 2 3 4 5 6 | List<Student> lstStudents = new List<Student> { new Student{Name="Marc",TotalMarks=774}, new Student{Name="John",TotalMarks=789}, new Student{Name="Amit",TotalMarks=888}, }; |

The sum of the scores of all the students can be calculated by using the sum function as:

1 | lstStudents.Sum(x => x.TotalMarks); |

We can write the above Sum() operator as:

1 | int sum = lstStudents.Select(x => x.TotalMarks).Sum(); |

Note that we have passed the lambda expression to the *Select()* operator instead of *Max()*.We can use the similar method overloads for each of the aggregate operators.

** Max **Many times when working with numeric values we need to find the maximum value from a collection of values.Max operator can be used in such a scenario to find the maximum value from a collection of values.Continuing with the example of collection of students ,the student having maximum score can be found as:

1 | int max = lstStudents.Max(x => x.TotalMarks); |

Above Max() function can also be written as:

1 | int max= lstStudents.Select(x=>x.TotalMarks).Max(); |

** Min **Sometimes it is required to identify an element having a minimum value in a collection of elements.Min operator is used to find a minimum value from a collection of values.

In the following example we have a collection of days and temperatures.

1 2 3 4 5 6 7 8 9 10 11 12 | Dictionary<int, int> tempratures = new Dictionary<int, int>(); tempratures.Add(1, 15); tempratures.Add(2, 18); tempratures.Add(3, 20); tempratures.Add(4, 16); tempratures.Add(5, 12); tempratures.Add(6, 19); tempratures.Add(7, 17); tempratures.Add(8, 26); tempratures.Add(9, 16); tempratures.Add(10, 19); |

The minimum temperature can be identified as:

1 | int min=tempratures.Min(x => x.Value); |

** Average **To calculate the average of numeric values in a collection we can use the Avg operator.

Continuing with the previous example of days and tempratures ,average temprature can be calculated as:

1 | double avg = tempratures.Average(x => x.Value); |

** Count **This returns the total number of elements in a collection.

Some of the main points about aggregate functions in LINQ are:

- Aggregate queries are executed immediately unlike other operators which are executed when the collection is iterated.
- Aggregate queries typically return a number instead of a collection.

## Leave a Reply