docs
docs copied to clipboard
The LINQ in "Create a nested group" example contains a superfluous group
The LINQ in "Create a nested group" example contains a superfluous group
: it contains three, but only two would be necessary. The third group regroups the first grouping, which has been ungrouped by the second group.
A better (more readable and more efficient) solution which is using two group
s:
var nestedGroupsQuery =
from student in students
group student by student.Year into StudentsByYear
select new
{
Year = StudentsByYear.Key,
StudentGroups =
from student in StudentsByYear
group student by student.LastName
};
foreach (var outerGroup in nestedGroupsQuery)
{
Console.WriteLine($"DataClass.Student Level = {outerGroup.Year}");
foreach (var innerGroup in outerGroup.StudentGroups)
{
Console.WriteLine($"\tNames that begin with: {innerGroup.Key}");
foreach (var innerGroupElement in innerGroup)
{
Console.WriteLine($"\t\t{innerGroupElement.LastName} {innerGroupElement.FirstName}");
}
}
}
A slightly longer (but IMHO even better) solution which uses proper naming everywhere instead of the not too descriptive Key
:
var nestedGroupsQuery =
from student in students
group student by student.Year into StudentsByYear
select new
{
Year = StudentsByYear.Key,
StudentGroups =
from student in StudentsByYear
group student by student.LastName into StudentsByLastName
select new
{
LastName = StudentsByLastName.Key,
Students = StudentsByLastName
}
};
foreach (var outerGroup in nestedGroupsQuery)
{
Console.WriteLine($"DataClass.Student Level = {outerGroup.Year}");
foreach (var innerGroup in outerGroup.StudentGroups)
{
Console.WriteLine($"\tNames that begin with: {innerGroup.LastName}");
foreach (var innerGroupElement in innerGroup.Students)
{
Console.WriteLine($"\t\t{innerGroupElement.LastName} {innerGroupElement.FirstName}");
}
}
}
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 728b8960-bd9b-6e47-0697-1ede60319dcc
- Version Independent ID: f2080e9b-3a61-ec89-eb34-b61aff1641fa
- Content: Create a nested group (LINQ in C#)
- Content Source: docs/csharp/linq/create-a-nested-group.md
- Product: dotnet-csharp
- Technology: csharp-linq
- GitHub Login: @BillWagner
- Microsoft Alias: wiwagn
What are your thoughts here @IEvangelist ?
I'm thinking that we should spend a bit of time cleaning up this content, I see a few opportunities:
-
We could strive to make all of the C# code examples here interactive (runnable in the browser).
-
We could encapsulate the common code into snippets (and includes) so that it can be re-rendered in each dependent article, rather than saying:
"The example in this topic uses the Student class and students list from the sample code in Query a collection of objects."
-
We could modernize the source code here:
- Instead of a
Student
class, lets usereadonly record struct
- Instead of a static property with a listing of
students
, maybe we could do something else?
- Instead of a
In looking at this, it does seem like the op has a valid point. Why show three group
clauses when two would work.
Also fixed in #39194