GroupBy
operator belong to Grouping Operators category. This operator takes a
flat sequence of items, organize that sequence into groups (IGrouping<K,V>)
based on a specific key and return groups of sequences.
In
short, GroupBy creates and returns a sequence of IGrouping<K,V>
Let us understand GroupBy with examples.
Let us understand GroupBy with examples.
Example 1: Get Employee Count By Department
var employeeGroup = from
employee in Employee.GetAllEmployees()
group employee by
employee.Department;
foreach (var group in employeeGroup)
{
Console.WriteLine("{0}
- {1}", group.Key, group.Count());
}
Example 2: Get Employee Count By Department and also each employee and
department name
var employeeGroup = from
employee in Employee.GetAllEmployees()
group employee by employee.Department;
foreach (var group in employeeGroup)
{
Console.WriteLine("{0}
- {1}", group.Key, group.Count());
Console.WriteLine("----------");
foreach (var employee
in group)
{
Console.WriteLine(employee.Name + "\t" + employee.Department);
}
Console.WriteLine(); Console.WriteLine();
}
Example 3: Get Employee Count By Department and also each employee and
department name. Data should be sorted first by Department in ascending order
and then by Employee Name in ascending order.
var employeeGroup = from
employee in Employee.GetAllEmployees()
group employee by employee.Department into
eGroup
orderby eGroup.Key
select new
{
Key = eGroup.Key,
Employees = eGroup.OrderBy(x =>
x.Name)
};
foreach (var group in employeeGroup)
{
Console.WriteLine("{0}
- {1}", group.Key, group.Employees.Count());
Console.WriteLine("----------");
foreach (var employee
in group.Employees)
{
Console.WriteLine(employee.Name + "\t" + employee.Department);
}
Console.WriteLine(); Console.WriteLine();
}Group by multiple keys in linq
In
LINQ, an anonymous type is usually used when we want to group by multiple keys.
Example 1:
Group employees by Department and then by Gender. The employee
groups should be sorted first by Department and then by Gender in
ascending order. Also, employees within each group must be sorted in ascending
order by Name.
var employeeGroups = Employee.GetAllEmployees()
.GroupBy(x => new
{ x.Department, x.Gender })
.OrderBy(g => g.Key.Department).ThenBy(g
=> g.Key.Gender)
.Select(g => new
{
Dept = g.Key.Department,
Gender = g.Key.Gender,
Employees = g.OrderBy(x =>
x.Name)
});
foreach(var group in employeeGroups)
{
Console.WriteLine("{0}
department {1} employees count = {2}",
group.Dept,
group.Gender, group.Employees.Count());
Console.WriteLine("--------------------------------------------");
foreach (var employee
in group.Employees)
{
Console.WriteLine(employee.Name + "\t" + employee.Gender
+ "\t" + employee.Department);
}
Console.WriteLine(); Console.WriteLine();
}
Example 2: Rewrite Example 1 using SQL like syntax
var employeeGroups = from employee in
Employee.GetAllEmployees()
group employee
by new
{
employee.Department,
employee.Gender
} into eGroup
orderby eGroup.Key.Department
ascending,
eGroup.Key.Gender ascending
eGroup.Key.Gender ascending
select new
{
Dept = eGroup.Key.Department,
Gender = eGroup.Key.Gender,
Employees = eGroup.OrderBy(x =>
x.Name)
};
0 comments:
Post a Comment