In LINQ, Aggregate() function is used to perform the operation on each item of the list. The Aggregate() function performs the action on the first and second elements and then carry forward the result. For the next operation, it will consider the previous result and the third element and then carryforwards, etc.
Example 1:
1 2 3 4 5 |
int[] Num = { 1, 2, 3, 4 }; double Average = Num.Aggregate((a, b) => a + b); Console.WriteLine("{0}", Average); //Output 10 ((1+2)+3)+4 |
Example 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Student collection IList<Student> studentList = new List<Student>>() { new Student() { StudentID = 1, StudentName = "John", Age = 13} , new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } , new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } , new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} , new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 } }; int SumOfStudentsAge = studentList.Aggregate<Student, int>(0, (totalAge, s) => totalAge += s.Age ); |
Example 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { //here we are creating the array Num type of int int[] Num = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Console.WriteLine("Product of the element:"); //Now we will calculate the average of the numbers by applying the Aggregate function double Average = Num.Aggregate((a, b) => a * b); Console.WriteLine("Product is {0}", Average); //Output 362880 ((((((((1*2)*3)*4)*5)*6)*7)*8)*9) //reate an array of string of the name charlist string[] charlist = { "a", "b", "c", "d" }; var concat = charlist.Aggregate((a, b) => a + ',' + b); Console.WriteLine("the Concatenated String: {0}", concat); // Output a,b,c,d Console.ReadLine(); } } } |
In the above examples, there is an integer array Num. We calculated the product of all the elements present in the given array. For this, we have to specify a Lambda Expression. In the Lambda Expression, we took two input parameters, “a” and “b.” And on the right-hand side, we multiply the parameters of input. Now we would be getting the product of all the numbers.
These are the steps that will describe the functionality of the above example.
- The first element 1 from the array is assigned to a. The second element 2 is assigned to b.
- The product of the two elements is calculated using Lambda Expression. The outcome of the first two-element (1 and 2) is stored in ‘a‘. Now the value of b is null.
- The first two elements have been used lambda will take the third element and assigned its value to b, which was null.
- Now “a” contains the product of the first two elements (1 and 2), and b contains the third (3) element. Now a and b multiplied according to lambda, and the resultant value is stored in a. Now b is set to null.
- The fourth element from the array is assigned to b and contains the product of the first three elements. This process will continue till the last element, and the product is finally displayed on the console.
In the same way, we are concatenating the list of items (a,b,c,d) in separated string in LINQ.
When we execute the above LINQ Aggregate () function, we will get the results as shown below:
Output:
Product of the element:
Product is 362880
the Concatenated String: a,b,c,d