0

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?

1 Answers1

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

Ken Hung
  • 752
  • 5
  • 13
  • Question about the first explanation (regarding the sub-query): I understand that using from genderGroup is different from using peopleList, but since you're grouping jobGroups by genderGroups later on anyway, isn't it a bit double to group them by Gender twice? – INEEDANSWERS Mar 18 '19 at 16:56
  • @INEEDANSWERS After `group person by person.Gender into genderGroup`, each `genderGroup` is iterated. If you use `peopleList` instead in the sub-query, the entire `peopleList` will be iterated for each gender group. Such that the same person will appear multiple times in the result (depending on how many genders are there). – Ken Hung Mar 20 '19 at 12:57