LINQ Aggregate Function

0
417

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

output:Output pages sum using foreach

 

and, using a recursive function

output:Output pages sum using foreach

And now, using Linq Aggregate function

output:Output pages sum using foreach

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 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.

output:Output for two parameters example

 

 

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 on it. At the example, I passed a lambda expression which calculates a book’s average pages that I should study in the next semester.

output:output for three argumnets