Open In App

LINQ | How to find aggregate of the given sequence?

Last Updated : 26 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In LINQ, you can create your own custom aggregation operation using the Aggregate method. This method allows you to perform a custom aggregation operation on the values of a collection or a sequence. It does not support query syntax in C# and VB.Net languages but can support method syntax in both languages. It present in both the Queryable and Enumerable class. This method is overloaded in 3 different ways:

  1. Aggregate<TSource, TAccumulate, TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate, TSource, TAccumulate>, Func<TAccumulate, TResult>): This method is used to apply an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.
  2. Aggregate<TSource, TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate, TSource, TAccumulate>): This method apply an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
  3. Aggregate<TSource>(IEnumerable<TSource>, Func<TSource, TSource, TSource>): This method apply an accumulator function over a sequence.

Example 1: 

CSharp




// C# program to illustrate the
// use of aggregate method
using System;
using System.Linq;
 
class GFG {
 
    // Main Method
    static public void Main()
    {
 
        // Data source
        string[] sequence = {"Geeks", "gfg",
                    "GeeksforGeeks", "GFG"};
 
        // Concatenate the element of the
        // given sequence using Aggregate function
        var result = sequence.Aggregate((q1, q2) => q1 + "-" + q2);
        Console.WriteLine(result);
    }
}


Output:

Geeks-gfg-GeeksforGeeks-GFG

Explanation: In var result = sequence.Aggregate((q1, q2) => q1+ “-“+q2);, first it takes Geeks and gfg elements and perform a concatenation operation in between them, then it takes this result to concatenate with GeeksforGeeks element. Again this result is used to perform the concatenation operation with GFG element and at last the final result is stored in the result variable. Or simply it is like: (((Geeks-gfg)-GeeksforGeeks)-GFG) Example 2: 

CSharp




// C# program to find the total
// salary of the employees
using System;
using System.Linq;
using System.Collections.Generic;
 
// Employee details
public class Employee {
 
    public int emp_id
    {
        get;
        set;
    }
 
    public string emp_name
    {
        get;
        set;
    }
 
    public string emp_gender
    {
        get;
        set;
    }
 
    public string emp_hire_date
    {
        get;
        set;
    }
 
    public int emp_salary
    {
        get;
        set;
    }
}
 
class GFG {
 
    // Main method
    static public void Main()
    {
        List<Employee> emp = new List<Employee>() {
 
            new Employee() {emp_id = 209, emp_name = "Anjita", emp_gender = "Female",
                                    emp_hire_date = "12/3/2017", emp_salary = 20000},
 
            new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female",
                                    emp_hire_date = "22/4/2018", emp_salary = 30000},
 
            new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male",
                                  emp_hire_date = "3/5/2016", emp_salary = 40000},
 
            new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female",
                                      emp_hire_date = "4/8/2017", emp_salary = 40000},
 
            new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male",
                                emp_hire_date = "12/1/2016", emp_salary = 40000},
 
            new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female",
                                  emp_hire_date = "17/6/2015", emp_salary = 50000},
        };
 
        // Query to find the total salary
        // of the employees
        // Using Aggregate method
        var res = emp.Select(e => e.emp_salary).Aggregate((x,
                                                y) => x + y);
                                                 
        Console.WriteLine("Total salary of the Employees: {0}", res);
    }
}


Output:

Total salary of the Employees: 220000

Explanation: The working of Aggregate method in this var res = emp.Select(e => e.emp_salary).Aggregate((x, y)=> x+y); statement is:

(20000, 30000) => 20000 + 30000; (50000, 40000) => 50000 + 40000; (50000, 40000) => 130000 + 40000; (130000, 40000) => 170000 + 40000; (170000, 50000) => 170000 + 50000; final value store in res is 220000

Reference:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads