Part 18 GroupBy in LINQ

23
11



Text version of the video

Slides

LINQ Tutorial – All Text Articles & Slides

LINQ Tutorial Playlist

Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses

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]

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();
}

Nguồn:https://wijstaanvooronzegrondrechten.org/

23 COMMENTS

  1. … and what if I want to retrieve the whole employee in the select new {} ? … linq keeps me upset. And no tutorial (yours included) have ever change my mind about the stupidity, unlogical "order" of its syntax. or maybe .. unlike many other tuto, you explain badly this one. because if I want to get back the un-ordered employee … syntax should be obvious. but I ca't even write "select employee" and egroup (if I understand well, is the equivalent of the employee.department) … so why does it return the whole employee ?
    Linq is a gadget, not technical, it requires no skills but memory.

  2. Who to print in this data in GridView anybody can explain please ?

    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();

    I am getting only Hr Record from below code
    var employeeGroup = from emp in Employee.GetAllEmployees()
    group emp by emp.Department;
    var result = string.Empty;

    foreach(var group in employeeGroup)
    {
    result = group.Key + " " + group.Count();

    }
    GridView1.DataSource = result;
    GridView1.DataBind();

  3. Hi Kudvenkat,

    I think there is no difference between GroupBy and ToLookup extension method , I could get the department and the employees who are working for that department by using the following Code :

    var result = Employee.GetAllEmployees().ToLookup(c => c.Department);

    And then we need to iterate through the result to get them but the same result with group by the department so, what do u think? Is there any difference?

  4. IEnumerable<IGrouping<string, Student>> students =  Student.GetAllStudents().GroupBy(x => x.City).OrderBy(x => x.Key);
                foreach (var group in students)
                {
                    Console.WriteLine(group.Key + " – " + group.Count());
                    Console.WriteLine("-——————-");
                    foreach (var student in group.OrderBy(x => x.Name))
                    {
                        Console.WriteLine('t' + student.Name);
                    }
                    Console.WriteLine(Environment.NewLine);
                }

  5. Venkat sir , how do u come to know our requirement …the level you use in your teaching is amazing because it gets easy to understand each module of the concept..the presentation and voice quality is awesome there is nothing stopping you to get award from Microsoft …this is our core desire …can we expect tute on sharepoint in future .
     

LEAVE A REPLY

Please enter your comment!
Please enter your name here