Aggregate is one of the extension methods which lives in System.Linq namespace, and applies to any object that implemented IEnumerable interface.

Aggregate is one of seven aggregation family functions which are:

- Min
- Max
- Count
- LongCount
- Sum
- Average
- Aggregate

All the other ones are so straight forward and I’ll talk about them in a post later.

You can make other six aggregation functions only by using the Aggregate since that one is provided to write custom aggregations, and the other ones are there to prevent redundant coding for common aggregations.

An easy and fast example for this function can be sum or multiplication of all numbers in an array.

let’s say, I have a list of ints which holds the number of pages for all the books that I’ll read in the next semester, and I want to know how many pages I should read in total.

We can solve this problem in three major ways:

- Using a loop
- Recursive function
- Linq aggregate function

First, I ‘ll show the solution using a foreach statement

1 2 3 4 5 6 7 8 9 |
int[] books = {100,200,300,400,500}; var total = 0; foreach (var book in books) { total += book; } Console.WriteLine($"I should read {total} pages in the next semester."); |

and, using a recursive function

1 2 3 4 5 6 |
static void Main(string[] args) { var total = CalculatePagesTotal(new List<int>() {100,200,300,400,500}); Console.WriteLine($"I should read {total} pages in the next semester."); } |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public static int CalculatePagesTotal(List<int> books ) { var book = books.FirstOrDefault(); if (books.Count != 0) { books.RemoveAt(0); return book + CalculatePagesTotal(books); } else { return 0; } } |

And now, using Linq Aggregate function

1 2 3 4 |
int[] books = {100,200,300,400,500}; var total = books.Aggregate((r, i) => r + i); Console.WriteLine($"I should read {total} pages in the next semester."); |

We can easily see how much less code you need to write to get the same result using the Aggregate function.

We are passing a lambda expression which has two arguments and doing the operation on those two arguments, but what are they? The first argument is the variable which will hold our calculation result and every time it will contain the previous calculation result, it means that we are repeating the same calculation with previous calculation result, and the second argument is our collection’s elements one at a time. It would be a valid question to ask, what is r (the first argument) at the first operation, and the answer is for the first r is our collection’s first element and i is the second element. But, that applies when you don’t provide a starting value for your r (result valuable)

## Overloads

We used one parameter overload for above example while this method has three overloads. The overload that we used for our example takes a lambda expression as the only parameter.

#### Two arguments overload example

The first parameter is the seed value and being used as a starting point for this example.

1 2 3 4 5 |
int[] books = {100,200,300}; var total = books.Aggregate(1000,(r, i) => r + i); Console.WriteLine($"Total: {total}"); |

#### Three arguments overload example

The last argument is also a Func, like the second argument, and we need to pass a lambda expression. We can use this argument to manipulate our final result and commit some changes to it. At the example, I passed a lambda expression which calculates a book’s average pages that I should study in the next semester.

1 2 3 4 5 |
int[] books = { 100, 200, 300 }; var total = books.Aggregate(0, (r, i) => r + i, r => r/(books.Length)); Console.WriteLine($"Avarage book has {total} pages."); |