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:
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.