Q: LINQ Nested Grouping - I don't understand the MSDN example As stated in the title, I cannot comprehend how the query in the below link works.
MSDN - LINQ Nested Grouping
Here is the query itself:
var queryNestedGroups =
    from student in students
    group student by student.Year into newGroup1
    from newGroup2 in
        (from student in newGroup1
         group student by student.LastName)
    group newGroup2 by newGroup1.Key;

I think the best approach would be me explaining what I understand and don't understand from this and maybe someone can lead me in the right direction.
OK, so my idea is:


*

*we create a couple of groups that contain objects of type student,

*[the inside of parenthesis] we take each of those groups and group the students inside them to create new groups,
(At this point we have x outer groups, each of them contain y inner groups.)


*

*[here let's just focus on one outer group] we take each newGroup2 from newGroup1 and group it by newGroup1's key.


But newGroup1 already has only students of one year, so newGroup2 also has only students of one year. Why do we need to group this again?

A: It may be easier to understand when presented in method syntax query:
var query = students.GroupBy(student => student.Year)
                    .SelectMany(
                        newGroup1 => newGroup1.GroupBy(student => student.LastName),
                        (newGroup1, newGroup2) => new { newGroup1, newGroup2 } )
                    .GroupBy(x => x.newGroup1.Key, x => x.newGroup2);

As you can see, from after group by is transformed into SelectMany so after that you have one big collection of anonymous type with two properties: newGroup1 and newGroup2. That's why you have to group is again using newGroup1.Key.
