# LINQ Aggregate Function with example

Keywords : How to use aggregate function in linq with example, Linq aggregate function tutorial with example, Linq aggregate function to add / multiply all elements in sequence with example

Here we will learn linq aggregate function / operator in c#, vb.net with examples and how to use linq aggregate function / operator to add all items in list or multiply all items in list or colleciton and how to convert list to comma separated string in linq or concatenate list of items into comma separated string with example in c#, vb.net.

## LINQ Aggregate() Function

In LINQ Aggregate function is used perform an operations on each element of the list. The Aggregate() function will perform the action on first and second element and then carry forward the result. For next operation it will consider previous result and the third element and then carry forwards, etc.

## Syntax of LINQ Aggregate Function

Following is the syntax of using LINQ Aggregate() function to perform addition of all numbers in the integer array.

C# Code

int[] Num = { 1, 2, 3, 4 };

double Average = Num.Aggregate((a, b) => a + b);

Console.WriteLine("{0}", Average); //Output 10 ((1+2)+3)+4

VB.NET Code

Dim Num As Integer() = {1, 2, 3, 4}

Dim Average As Double = Num.Aggregate(Function(a, b) a + b)

Console.WriteLine("{0}", Average) //Output 10 ((1+2)+3)+4

Above syntax will take first two elements  1 and  2 to perform addition and make  3 then it take previous result 3 and next element 3 and perform addition to make 6 and then add 6 to next element 4 and result will be 10.

## LINQ Aggregate() Function Example

Now we will see the example of using linq Aggregate() function to calculate the product of all the numbers in the integer array.

C# Code

using System;

using System.Collections.Generic;

using System.Linq;

namespace Linqtutorials

{

classProgram

{

staticvoid Main(string[] args)

{

int[] Num = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

Console.WriteLine("Find the Product of the elements:");

double Average = Num.Aggregate((a, b) => a * b);

Console.WriteLine("The Product is {0}", Average); //Output 362880 ((((((((1*2)*3)*4)*5)*6)*7)*8)*9)

string[] charlist = { "a", "b", "c", "d" };

var concta = charlist.Aggregate((a, b) => a + ',' + b);

Console.WriteLine("Concatenated String: {0}",concta); // Output a,b,c,d

}

}

}

VB.NET Code

Module Module1

Sub Main()

Dim Num As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9}

Console.WriteLine("Find the Product of the elements:")

Dim Average As Double = Num.Aggregate(Function(a, b) a * b)

Console.WriteLine("The Product is {0}", Average) 'Output 362880 ((((((((1*2)*3)*4)*5)*6)*7)*8)*9)

Dim charlist As String() = {"a", "b", "c", "d"}

Dim concta = charlist.Aggregate(Function(a, b) a + ","c + b)

Console.WriteLine("Concatenated String: {0}", concta) ' Output a,b,c,d

End Sub

End Module

In the above code we have an integer array Num. We will calculate the product of all the elements present in the given array. To do this we have to specify a Lambda expression using Aggregate function. In the Lambda expression we take two input parameters "a" and "b". And on the right hand side we simply multiply the input parameters. So we would be getting the product of all the numbers. But wait, how this is possible?  We have just two input parameters and much more elements in the array. So how does Lambda work in this scenario?

The following steps describe the functioning of the above example.

1. The first element "1" from the array is assigned to "a". The second element "2" is assigned to "b".
2. The product of the two elements is calculated using Lambda expression. This product of first two elements ("1" and "2") is now stored in "a". The value of "b" is now null.
3. Since the first two elements have been used lambda will take the third element and assign its value to "b" which was null.
4. Now "a" contains the product of first two elements ("1" and "2") and "b" contains the third element ("3").  Now "a" and "b" gets multiplied according to the lambda and the resultant value is now stored in "a". Now "b" is set to null.
5. The fourth element from the array gets assigned to "b" now. Now "a" contains the product of first three elements. This process continues till the last element and the product is finally displayed on the console.

Same way we are concatenating list of items (a,b,c,d) into comma separated string in linq.

So we just saw how simple it is to calculate the product of the elements in the integer array. If one uses a normal non LINQ approach then we have to write a longer code but here it is just one line of code that does the trick.

## Output of LINQ Aggregate() Function Example

Following is the result of using LINQ Aggregate() function to perform operations on all elements in list.

Find the Product of the elements:

The Product is 362800

Concatenated String: a,b,c,d

This is how we can use LINQ Aggregate() function in applications to perform operations on elements in list or collection object based on our requirements.