Author : Rahul
Last Modified : 11-Jul-2021
Complexity : Beginner

Aggregate Operator in LINQ


Aggregate Operator in LINQ is used to perform some mathematical operations like min, max, count, sum, average, aggregate on number value within the collection. There are different Aggregate Operators are available in LINQ as follow:

  1. COUNT
  2. SUM
  3. MIN
  4. MAX
  5. AVERAGE
  6. AGGREGATE

COUNT Operator

COUNT Operator is used to count the number of elements within the collection. Count keyword is used for this purpose. Below are some examples in this category.


Query to count the number of elements in the array.

 class Program
 {
   static void Main(string[] args)
   {
     int[] numbers = { 3, 6, 9, 12, 15 };
     
     //Linq Query
     var result_Linq = (from p in numbers select p).Count();
     
     //Lambda Expression
     var result_Lambda = numbers.Count();
     
     //Display Linq Query Result
     Console.WriteLine($"Number of elements in the array (Using Linq): {result_Linq}");
     
     //Display Lambda Expression Result
     Console.WriteLine($"Number of elements in the array (Using Lambda): {result_Lambda}");
     
     Console.Read();
   }
 }

Output

Number of elements in the array (Using Linq): 5
Number of elements in the array (Using Lambda): 5

 

Query to get the number of characters in each word in the array.

 class Program
 {
   static void Main(string[] args)
   {
     string[] words = { "rahul", "kumar", "goel", "delhi" };
     
     //Linq Query
     var result_Linq = from p in words select new { Word = p, WordLength = p.Count() };
     
     //Lambda Expression
     var result_Lambda = words.Select(p => new { Word = p, WordLength = p.Count() });
     
     //Display Linq Query Result
     Console.WriteLine("Number of character in each word (Using Linq):");
     foreach(var item in result_Linq)
     {
       Console.WriteLine($"Word: {item.Word}, Word Length: {item.WordLength}");
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Number of character in each word (Using Lambda):");
     foreach (var item in result_Lambda)
     {
       Console.WriteLine($"Word: {item.Word}, Word Length: {item.WordLength}");
     }
     
     Console.Read();
   }
 }

Output

Number of character in each word (Using Linq):
Word: rahul, Word Length: 5
Word: kumar, Word Length: 5
Word: goel, Word Length: 4
Word: delhi, Word Length: 5
Number of character in each word (Using Lambda):
Word: rahul, Word Length: 5
Word: kumar, Word Length: 5
Word: goel, Word Length: 4
Word: delhi, Word Length: 5

 

Query to return a list of departments and the number of employees in each department.

 public class Employee
 {
   public string Emp_Name { get; set; }
   public string Emp_Department { get; set; }
   public int Emp_Salary { get; set; }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     List<Employee> employees = GetEmployeeList();
     
     //Linq Query
     var result_Linq = from p in employees group p by p.Emp_Department into g select new { Department = g.Key, EmployeeCount = g.Count() };
     
     //Lambda Expression
     var result_Lambda = employees.GroupBy(p => p.Emp_Department).Select(g => new { Department = g.Key, EmployeeCount = g.Count() });
     
     //Display Linq Query Result
     Console.WriteLine("Employee details against each department (Using Linq):");
     foreach(var item in result_Linq)
     {
       Console.WriteLine($"Department: {item.Department}, Employee Count: {item.EmployeeCount}");
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Employee details against each department (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine($"Department: {item.Department}, Employee Count: {item.EmployeeCount}");
     }
     
     Console.Read();
   }
   
   /// <summary>
   /// Method to get employee list
   /// </summary>
   /// <returns></returns>
   private static List<Employee> GetEmployeeList()
   {
     List<Employee> employees = new List<Employee>();
     employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Rajesh", Emp_Salary = 40000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 60000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
     employees.Add(new Employee() { Emp_Name = "Neeraj", Emp_Salary = 40000, Emp_Department = "Account" });
     return employees;
   }
 }

Output

Employee details against each department (Using Linq):
Department: IT, Employee Count: 2
Department: HR, Employee Count: 2
Department: Account, Employee Count: 2
Employee details against each department (Using Linq):
Department: IT, Employee Count: 2
Department: HR, Employee Count: 2
Department: Account, Employee Count: 2

 

SUM Operator

SUM Operator is used to calculate the sum of the values within the collection. Sum keyword is used for this purpose. Below are some examples in this category.

Query to get the sum of the elements in the array.

 class Program
 {
   static void Main(string[] args)
   {
     int[] numbers = { 3, 6, 9, 12, 15 };
     
     //Linq Query
     var result_Linq = (from p in numbers select p).Sum();
     
     //Lambda Expression
     var result_Lambda = numbers.Sum();
     
     //Display Linq Query Result
     Console.WriteLine($"Sum of numbers in the array (Using Linq): {result_Linq}");
     
     //Display Lambda Expression Result
     Console.WriteLine($"Sum of numbers in the array (Using Lambda): {result_Lambda}");
     
     Console.Read();
   }
 }

Output

Sum of numbers in the array (Using Linq): 45
Sum of numbers in the array (Using Lambda): 45

 

Query to get the total number of characters in the array.

 class Program
 {
   static void Main(string[] args)
   {
     string[] words = { "rahul", "kumar", "goel", "delhi" };
     
     //Linq Query
     var result_Linq = (from p in words select p.Count()).Sum();
     
     //Lambda Expression
     var result_Lambda = words.Sum(p => p.Count());
     
     //Display Linq Query Result
     Console.WriteLine($"Total Number of characters in the array (Using Linq): {result_Linq}");
     
     //Display Lambda Expression Result
     Console.WriteLine($"Total Number of characters in the array (Using Lambda): {result_Lambda}");
     
     Console.Read();
   }
 }

Output

Total Number of characters in the array (Using Linq): 19
Total Number of characters in the array (Using Lambda): 19

 

Query to return a list of departments and the sum of the salary in each department.

 public class Employee
 {
   public string Emp_Name { get; set; }
   public string Emp_Department { get; set; }
   public int Emp_Salary { get; set; }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     List<Employee> employees = GetEmployeeList();
     
     //Linq Query
     var result_Linq = from p in employees group p by p.Emp_Department into g select new { Department = g.Key, TotalSalary = g.Sum(p=>p.Emp_Salary) };
     
     //Lambda Expression
     var result_Lambda = employees.GroupBy(p => p.Emp_Department).Select(g => new { Department = g.Key, TotalSalary = g.Sum(p=>p.Emp_Salary) });
     
     //Display Linq Query Result
     Console.WriteLine("Total Salary against each department (Using Linq):");
     foreach(var item in result_Linq)
     {
       Console.WriteLine($"Department: {item.Department}, Total Salary: {item.TotalSalary}");
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Total Salary against each department (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine($"Department: {item.Department}, Total Salary: {item.TotalSalary}");
     }
     
     Console.Read();
   }
   
   /// <summary>
   /// Method to get employee list
   /// </summary>
   /// <returns></returns>
   private static List<Employee> GetEmployeeList()
   {
     List<Employee> employees = new List<Employee>();
     employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Rajesh", Emp_Salary = 40000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 60000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
     employees.Add(new Employee() { Emp_Name = "Neeraj", Emp_Salary = 40000, Emp_Department = "Account" });
     return employees;
   }
 }

Output

Total Salary against each department (Using Linq):
Department: IT, Total Salary: 130000
Department: HR, Total Salary: 60000
Department: Account, Total Salary: 120000
Total Salary against each department (Using Linq):
Department: IT, Total Salary: 130000
Department: HR, Total Salary: 60000
Department: Account, Total Salary: 120000


MIN Operator

MIN Operator is used to find the lowest value within the collection. Min keyword is used for this purpose. Below are some examples in this category.

Query to get the lowest number in the array.

 class Program
 {
   static void Main(string[] args)
   {
     int[] numbers = { 3, 6, 9, 12, 15 };
     
     //Linq Query
     var result_Linq = (from p in numbers select p).Min();
     
     //Lambda Expression
     var result_Lambda = numbers.Min();
     
     //Display Linq Query Result
     Console.WriteLine($"Minimum Number in the array (Using Linq): {result_Linq}");
     
     //Display Lambda Expression Result
     Console.WriteLine($"Minimum Number in the array (Using Lambda): {result_Lambda}");
     
     Console.Read();
   }
 }

Output

Minimum Number in the array (Using Linq): 3
Minimum Number in the array (Using Lambda): 3

 

Query to get the length of the smallest word in the array.

 class Program
 {
   static void Main(string[] args)
   {
     string[] words = { "rahul", "kumar", "goel", "delhi" };
     
     //Linq Query
     var result_Linq = (from p in words select p.Count()).Min();
     
     //Lambda Expression
     var result_Lambda = words.Min(p => p.Count());
     
     //Display Linq Query Result
     Console.WriteLine($"Minimum Number of characters in the array (Using Linq): {result_Linq}");
     
     //Display Lambda Expression Result
     Console.WriteLine($"Minimum Number of characters in the array (Using Lambda): {result_Lambda}");
     
     Console.Read();
   }
 }

Output

Minimum Number of characters in the array (Using Linq): 4
Minimum Number of characters in the array (Using Lambda): 4

 

Query to return a list of departments and the smallest salary in each department.

 public class Employee
 {
   public string Emp_Name { get; set; }
   public string Emp_Department { get; set; }
   public int Emp_Salary { get; set; }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     List<Employee> employees = GetEmployeeList();
     
     //Linq Query
     var result_Linq = from p in employees group p by p.Emp_Department into g select new { Department = g.Key, MinimumSalary = g.Min(p=>p.Emp_Salary) };
     
     //Lambda Expression
     var result_Lambda = employees.GroupBy(p => p.Emp_Department).Select(g => new { Department = g.Key, MinimumSalary = g.Min(p=>p.Emp_Salary) });
     
     //Display Linq Query Result
     Console.WriteLine("Minimum Salary against each department (Using Linq):");
     foreach(var item in result_Linq)
     {
       Console.WriteLine($"Department: {item.Department}, Minimum Salary: {item.MinimumSalary}");
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Minimum Salary against each department (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine($"Department: {item.Department}, Minimum Salary: {item.MinimumSalary}");
     }
     
     Console.Read();
   }
   
   /// <summary>
   /// Method to get employee list
   /// </summary>
   /// <returns></returns>
   private static List<Employee> GetEmployeeList()
   {
     List<Employee> employees = new List<Employee>();
     employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Rajesh", Emp_Salary = 40000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 60000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
     employees.Add(new Employee() { Emp_Name = "Neeraj", Emp_Salary = 40000, Emp_Department = "Account" });
     return employees;
   }
 }

Output

Minimum Salary against each department (Using Linq):
Department: IT, Minimum Salary: 60000
Department: HR, Minimum Salary: 20000
Department: Account, Minimum Salary: 40000
Minimum Salary against each department (Using Linq):
Department: IT, Minimum Salary: 60000
Department: HR, Minimum Salary: 20000
Department: Account, Minimum Salary: 40000

 

Query to return a list of department and employee details with the smallest salary in each department.

 public class Employee
 {
   public string Emp_Name { get; set; }
   public string Emp_Department { get; set; }
   public int Emp_Salary { get; set; }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     List<Employee> employees = GetEmployeeList();
     
     //Linq Query
     var result_Linq = from p in employees group p by p.Emp_Department into g
                       let minsalary = g.Min(p=>p.Emp_Salary)
                       select new { Department = g.Key, EmployeeDetail = g.Where(p=>p.Emp_Salary == minsalary) };
                       
     //Lambda Expression
     var result_Lambda = employees.GroupBy(p => p.Emp_Department).
       Select(g => new { Dept = g.Key, AllEmployees = g, MinimumSalary = g.Min(p => p.Emp_Salary) }).
       Select(@t => new { Department = t.Dept, EmployeeDetail = t.AllEmployees.Where(p => p.Emp_Salary == t.MinimumSalary) });
       
     //Display Linq Query Result
     Console.WriteLine("Employee Details against each department (Using Linq):");
     foreach(var item in result_Linq)
     {
       Console.WriteLine($"Department Name: {item.Department}");
       foreach(var emp in item.EmployeeDetail)
       {
         Console.WriteLine($"Employee Name: {emp.Emp_Name}, Employee Department: {emp.Emp_Department}, Employee Salary: {emp.Emp_Salary}");
       }
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Employee Details against each department (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine($"Department Name: {item.Department}");
       foreach (var emp in item.EmployeeDetail)
       {
         Console.WriteLine($"Employee Name: {emp.Emp_Name}, Employee Department: {emp.Emp_Department}, Employee Salary: {emp.Emp_Salary}");
       }
     }
     
     Console.Read();
   }
   
   /// <summary>
   /// Method to get employee list
   /// </summary>
   /// <returns></returns>
   private static List<Employee> GetEmployeeList()
   {
     List<Employee> employees = new List<Employee>();
     employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Rajesh", Emp_Salary = 40000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 60000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
     employees.Add(new Employee() { Emp_Name = "Neeraj", Emp_Salary = 40000, Emp_Department = "Account" });
     return employees;
   }
 }

Output

Employee Details against each department (Using Linq):
Department Name: IT
Employee Name: Suresh, Employee Department: IT, Employee Salary: 60000
Department Name: HR
Employee Name: Amit, Employee Department: HR, Employee Salary: 20000
Department Name: Account
Employee Name: Neeraj, Employee Department: Account, Employee Salary: 40000
Employee Details against each department (Using Linq):
Department Name: IT
Employee Name: Suresh, Employee Department: IT, Employee Salary: 60000
Department Name: HR
Employee Name: Amit, Employee Department: HR, Employee Salary: 20000
Department Name: Account
Employee Name: Neeraj, Employee Department: Account, Employee Salary: 40000

 

MAX Operator

MAX Operator is used to find the largest value within the collection. Max keyword is used for this purpose. Below are some examples in this category.

Query to get the largest number in the array.

 class Program
 {
   static void Main(string[] args)
   {
     int[] numbers = { 3, 6, 9, 12, 15 };
     
     //Linq Query
     var result_Linq = (from p in numbers select p).Max();
     
     //Lambda Expression
     var result_Lambda = numbers.Max();
     
     //Display Linq Query Result
     Console.WriteLine($"Maximum Number in the array (Using Linq): {result_Linq}");
     
     //Display Lambda Expression Result
     Console.WriteLine($"Maximum Number in the array (Using Lambda): {result_Lambda}");
     
     Console.Read();
   }
 }

Output

Maximum Number in the array (Using Linq): 15
Maximum Number in the array (Using Lambda): 15

 

Query to get the length of the largest word in the array.

class Program
 {
   static void Main(string[] args)
   {
     string[] words = { "rahul", "kumar", "goel", "delhi" };
     
     //Linq Query
     var result_Linq = (from p in words select p.Count()).Max();
     
     //Lambda Expression
     var result_Lambda = words.Max(p => p.Count());
     
     //Display Linq Query Result
     Console.WriteLine($"Maximum Number of characters in the array (Using Linq): {result_Linq}");
     
     //Display Lambda Expression Result
     Console.WriteLine($"Maximum Number of characters in the array (Using Lambda): {result_Lambda}");
     
     Console.Read();
   }
 }

Output

Maximum Number of characters in the array (Using Linq): 5
Maximum Number of characters in the array (Using Lambda): 5

 

Query to return a list of departments and the largest salary in each department.

public class Employee
 {
   public string Emp_Name { get; set; }
   public string Emp_Department { get; set; }
   public int Emp_Salary { get; set; }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     List<Employee> employees = GetEmployeeList();
     
     //Linq Query
     var result_Linq = from p in employees group p by p.Emp_Department into g select new { Department = g.Key, MaximumSalary = g.Max(p=>p.Emp_Salary) };
     
     //Lambda Expression
     var result_Lambda = employees.GroupBy(p => p.Emp_Department).Select(g => new { Department = g.Key, MaximumSalary = g.Max(p=>p.Emp_Salary) });
     
     //Display Linq Query Result
     Console.WriteLine("Maximum Salary against each department (Using Linq):");
     foreach(var item in result_Linq)
     {
       Console.WriteLine($"Department: {item.Department}, Maximum Salary: {item.MaximumSalary}");
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Maximum Salary against each department (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine($"Department: {item.Department}, Maximum Salary: {item.MaximumSalary}");
     }
     
     Console.Read();
   }
   
   /// <summary>
   /// Method to get employee list
   /// </summary>
   /// <returns></returns>
   private static List<Employee> GetEmployeeList()
   {
     List<Employee> employees = new List<Employee>();
     employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Rajesh", Emp_Salary = 40000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 60000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
     employees.Add(new Employee() { Emp_Name = "Neeraj", Emp_Salary = 40000, Emp_Department = "Account" });
     return employees;
   }
 }

Output

Maximum Salary against each department (Using Linq):
Department: IT, Maximum Salary: 70000
Department: HR, Maximum Salary: 40000
Department: Account, Maximum Salary: 80000
Maximum Salary against each department (Using Linq):
Department: IT, Maximum Salary: 70000
Department: HR, Maximum Salary: 40000
Department: Account, Maximum Salary: 80000

 

Query to return a list of department and employee details with the largest salary in each department.

 public class Employee
 {
   public string Emp_Name { get; set; }
   public string Emp_Department { get; set; }
   public int Emp_Salary { get; set; }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     List<Employee> employees = GetEmployeeList();
     
     //Linq Query
     var result_Linq = from p in employees group p by p.Emp_Department into g
                       let maxsalary = g.Max(p=>p.Emp_Salary)
                       select new { Department = g.Key, EmployeeDetail = g.Where(p=>p.Emp_Salary == maxsalary) };
                       
     //Lambda Expression
     var result_Lambda = employees.GroupBy(p => p.Emp_Department).
       Select(g => new { Dept = g.Key, AllEmployees = g, MaximumSalary = g.Max(p => p.Emp_Salary) }).
       Select(@t => new { Department = t.Dept, EmployeeDetail = t.AllEmployees.Where(p => p.Emp_Salary == t.MaximumSalary) });
       
     //Display Linq Query Result
     Console.WriteLine("Employee Details against each department (Using Linq):");
     foreach(var item in result_Linq)
     {
       Console.WriteLine($"Department Name: {item.Department}");
       foreach(var emp in item.EmployeeDetail)
       {
         Console.WriteLine($"Employee Name: {emp.Emp_Name}, Employee Department: {emp.Emp_Department}, Employee Salary: {emp.Emp_Salary}");
       }
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Employee Details against each department (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine($"Department Name: {item.Department}");
       foreach (var emp in item.EmployeeDetail)
       {
         Console.WriteLine($"Employee Name: {emp.Emp_Name}, Employee Department: {emp.Emp_Department}, Employee Salary: {emp.Emp_Salary}");
       }
     }
     
     Console.Read();
   }
   
   /// <summary>
   /// Method to get employee list
   /// </summary>
   /// <returns></returns>
   private static List<Employee> GetEmployeeList()
   {
     List<Employee> employees = new List<Employee>();
     employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Rajesh", Emp_Salary = 40000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 60000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
     employees.Add(new Employee() { Emp_Name = "Neeraj", Emp_Salary = 40000, Emp_Department = "Account" });
     return employees;
   }
 }

Output

Employee Details against each department (Using Linq):
Department Name: IT
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
Department Name: HR
Employee Name: Rajesh, Employee Department: HR, Employee Salary: 40000
Department Name: Account
Employee Name: Nitish, Employee Department: Account, Employee Salary: 80000
Employee Details against each department (Using Linq):
Department Name: IT
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
Department Name: HR
Employee Name: Rajesh, Employee Department: HR, Employee Salary: 40000
Department Name: Account
Employee Name: Nitish, Employee Department: Account, Employee Salary: 80000

 

AVERAGE Operator

AVERAGE Operator is used to calculate the average of the values within the collection. Average keyword is used for this purpose. Below are some examples in this category.

Query to get the average number in the array.

 class Program
 {
   static void Main(string[] args)
   {
     int[] numbers = { 3, 6, 9, 12, 15 };
     
     //Linq Query
     var result_Linq = (from p in numbers select p).Average();
     
     //Lambda Expression
     var result_Lambda = numbers.Average();
     
     //Display Linq Query Result
     Console.WriteLine($"Average Number in the array (Using Linq): {result_Linq}");
     
     //Display Lambda Expression Result
     Console.WriteLine($"Average Number in the array (Using Lambda): {result_Lambda}");
     
     Console.Read();
   }
 }

Output

Average Number in the array (Using Linq): 9
Average Number in the array (Using Lambda): 9

 

Query to get the average length of the words in the array.

 class Program
 {
   static void Main(string[] args)
   {
     string[] words = { "rahul", "kumar", "goel", "delhi" };
     
     //Linq Query
     var result_Linq = (from p in words select p.Count()).Average();
     
     //Lambda Expression
     var result_Lambda = words.Average(p => p.Count());
     
     //Display Linq Query Result
     Console.WriteLine($"Average Number of characters in the array (Using Linq): {result_Linq}");
     
     //Display Lambda Expression Result
     Console.WriteLine($"Average Number of characters in the array (Using Lambda): {result_Lambda}");
     
     Console.Read();
   }
 }

Output

Average Number of characters in the array (Using Linq): 4.75
Average Number of characters in the array (Using Lambda): 4.75

 

Query to return a list of departments and average salaries in each department.

 public class Employee
 {
   public string Emp_Name { get; set; }
   public string Emp_Department { get; set; }
   public int Emp_Salary { get; set; }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     List<Employee> employees = GetEmployeeList();
     
     //Linq Query
     var result_Linq = from p in employees group p by p.Emp_Department into g select new { Department = g.Key, AverageSalary = g.Average(p=>p.Emp_Salary) };
     
     //Lambda Expression
     var result_Lambda = employees.GroupBy(p => p.Emp_Department).Select(g => new { Department = g.Key, AverageSalary = g.Average(p=>p.Emp_Salary) });
     
     //Display Linq Query Result
     Console.WriteLine("Average Salary against each department (Using Linq):");
     foreach(var item in result_Linq)
     {
       Console.WriteLine($"Department: {item.Department}, Average Salary: {item.AverageSalary}");
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Average Salary against each department (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine($"Department: {item.Department}, Average Salary: {item.AverageSalary}");
     }
     
     Console.Read();
   }
   
   /// <summary>
   /// Method to get employee list
   /// </summary>
   /// <returns></returns>
   private static List<Employee> GetEmployeeList()
   {
     List<Employee> employees = new List<Employee>();
     employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Rajesh", Emp_Salary = 40000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 60000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
     employees.Add(new Employee() { Emp_Name = "Neeraj", Emp_Salary = 40000, Emp_Department = "Account" });
     return employees;
   }
 }

Output

Average Salary against each department (Using Linq):
Department: IT, Average Salary: 65000
Department: HR, Average Salary: 30000
Department: Account, Average Salary: 60000
Average Salary against each department (Using Linq):
Department: IT, Average Salary: 65000
Department: HR, Average Salary: 30000
Department: Account, Average Salary: 60000

 

Query to return a list of department and employee details whose salary is greater than the average salary in each department.

 public class Employee
 {
   public string Emp_Name { get; set; }
   public string Emp_Department { get; set; }
   public int Emp_Salary { get; set; }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     List<Employee> employees = GetEmployeeList();
     
     //Linq Query
     var result_Linq = from p in employees group p by p.Emp_Department into g
                       let avgsalary = g.Average(p=>p.Emp_Salary)
                       select new { Department = g.Key, EmployeeDetail = g.Where(p=>p.Emp_Salary > avgsalary) };
                       
     //Lambda Expression
     var result_Lambda = employees.GroupBy(p => p.Emp_Department).
       Select(g => new { Dept = g.Key, AllEmployees = g, AverageSalary = g.Average(p => p.Emp_Salary) }).
       Select(@t => new { Department = t.Dept, EmployeeDetail = t.AllEmployees.Where(p => p.Emp_Salary > t.AverageSalary) });
       
     //Display Linq Query Result
     Console.WriteLine("Employee Details against each department (Using Linq):");
     foreach(var item in result_Linq)
     {
       Console.WriteLine($"Department Name: {item.Department}");
       foreach(var emp in item.EmployeeDetail)
       {
         Console.WriteLine($"Employee Name: {emp.Emp_Name}, Employee Department: {emp.Emp_Department}, Employee Salary: {emp.Emp_Salary}");
       }
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Employee Details against each department (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine($"Department Name: {item.Department}");
       foreach (var emp in item.EmployeeDetail)
       {
         Console.WriteLine($"Employee Name: {emp.Emp_Name}, Employee Department: {emp.Emp_Department}, Employee Salary: {emp.Emp_Salary}");
       }
     }
     
     Console.Read();
   }
   
   /// <summary>
   /// Method to get employee list
   /// </summary>
   /// <returns></returns>
   private static List<Employee> GetEmployeeList()
   {
     List<Employee> employees = new List<Employee>();
     employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Rajesh", Emp_Salary = 40000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 60000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
     employees.Add(new Employee() { Emp_Name = "Neeraj", Emp_Salary = 40000, Emp_Department = "Account" });
     return employees;
   }
 }

Output

Employee Details against each department (Using Linq):
Department Name: IT
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
Department Name: HR
Employee Name: Rajesh, Employee Department: HR, Employee Salary: 40000
Department Name: Account
Employee Name: Nitish, Employee Department: Account, Employee Salary: 80000
Employee Details against each department (Using Linq):
Department Name: IT
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
Department Name: HR
Employee Name: Rajesh, Employee Department: HR, Employee Salary: 40000
Department Name: Account
Employee Name: Nitish, Employee Department: Account, Employee Salary: 80000

 

AGGREGATE Operator

AGGREGATE Operator is used to perform some operations on the values within the collection. Aggregate keyword is used for this purpose. Below are some examples in this category.

Query to calculate the running product of all elements in the array.

class Program
 {
   static void Main(string[] args)
   {
     int[] numbers = { 3, 6, 8, 9 };
     
     //Linq Query
     var result_Linq = numbers.Aggregate((product, nextNum) => product * nextNum);
     
     //Lambda Expression
     var result_Lambda = numbers.Aggregate((product, nextNum) => product * nextNum);
     
     //Display Linq Query Result
     Console.WriteLine($"Total product of all numbers (Using Linq): {result_Linq}");
     
     //Display Lambda Expression Result
     Console.WriteLine($"Total product of all numbers (Using Lambda): {result_Lambda}");
     
     Console.Read();
   }
 }

Output

Total product of all numbers (Using Linq): 1296
Total product of all numbers (Using Lambda): 1296