This Angular tutorial helps you get started with Angular quickly and effectively through many practical examples.

LINQ GroupBy Query


Here are some common LINQ queries related to GroupBy, demonstrating how to group data by specific criteria and perform aggregations or transformations on those groups:

1. Basic Group By

Group items by a single key and select the groups.

var employees = new List<Employee>
{
    new Employee { Department = "HR", Name = "Rohatash" },
    new Employee { Department = "IT", Name = "Mohit" },
    new Employee { Department = "HR", Name = "Jeet" }
};

var groupedByDepartment = from emp in employees
                          group emp by emp.Department into departmentGroup
                          select new
                          {
                              Department = departmentGroup.Key,
                              Employees = departmentGroup.ToList()
                          };

// Output: { Department = "HR", Employees = [{ Name = "Rohatash" }, { Name = "Jeet" }] },
//         { Department = "IT", Employees = [{ Name = "Mohit" }] }

2. Group By with Aggregation

Group items and compute aggregate values for each group.

var orders = new List<Order>
{
    new Order { Category = "Electronics", Amount = 100 },
    new Order { Category = "Clothing", Amount = 50 },
    new Order { Category = "Electronics", Amount = 150 },
    new Order { Category = "Clothing", Amount = 75 }
};

var totalSalesByCategory = from order in orders
                           group order by order.Category into categoryGroup
                           select new
                           {
                               Category = categoryGroup.Key,
                               TotalSales = categoryGroup.Sum(o => o.Amount)
                           };

// Output: { Category = "Electronics", TotalSales = 250 },
//         { Category = "Clothing", TotalSales = 125 }

3. Group By with Filtering

Group items and then filter the results within each group.

var employees = new List<Employee>
{
    new Employee { Department = "HR", Name = "Alice", Salary = 60000 },
    new Employee { Department = "IT", Name = "Bob", Salary = 50000 },
    new Employee { Department = "HR", Name = "Charlie", Salary = 70000 },
    new Employee { Department = "IT", Name = "David", Salary = 55000 }
};

var highEarnersByDepartment = from emp in employees
                              group emp by emp.Department into departmentGroup
                              let highEarners = departmentGroup.Where(e => e.Salary > 55000)
                              where highEarners.Any()
                              select new
                              {
                                  Department = departmentGroup.Key,
                                  HighEarners = highEarners.ToList()
                              };

// Output: { Department = "HR", HighEarners = [{ Name = "Charlie", Salary = 70000 }] },
//         { Department = "IT", HighEarners = [{ Name = "David", Salary = 55000 }] }



Prev Next