Search code examples
c#linqgroupingnested-groups

LINQ Nested group syntax


MSDN does a really bad job at explaining nested groups in LINQ: https://learn.microsoft.com/en-us/dotnet/csharp/linq/create-a-nested-group

Take this example:

IEnumerable<IGrouping<Gender, IGrouping<Job, Person>>> query =
    from person in peopleList
    group person by person.Gender into genderGroup
    from jobGroup (
      from person in genderGroup
      group person by person.Job
    )
    group jobGroup by genderGroup.Key;

My questions:

  1. In the from clause between parentheses, why does the source have to be genderGroup? Why can't it just be from peopleList like in the first from clause?
  2. In the last clause, which property of the jobGroup is actually being looked at when it is compared with genderGroup.Keys?

Solution

  • from person in genderGroup
    group person by person.Job
    

    This sub-query iterates people of the same gender, so it groups males by their jobs, and then group females by their jobs, and so on. If peopleList is used instead of genderGroup, the query will just group student by jobs and there will be no nested grouping.

    group jobGroup by genderGroup.Key;
    

    This groups the inner groups rather than people by the outer group's key (gender). Suppose there are four jobGroup: male programmers, male designers, female programmers, and female designers. The query groups these four groups by gender and hence produces a nested group:

    male (outer group's key)
        male programmers 
        male designers
    female (outer group's key)
        female programmers 
        female designers
    

    To have a better understanding, I recommend you run this small code in Visual Studio and set breakpoints inside the LINQ statement to see what happens.