docs icon indicating copy to clipboard operation
docs copied to clipboard

The LINQ in "Create a nested group" example contains a superfluous group

Open davidnemeti opened this issue 1 year ago • 2 comments

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

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.

davidnemeti avatar Jul 28 '22 13:07 davidnemeti

What are your thoughts here @IEvangelist ?

BillWagner avatar Jul 28 '22 14:07 BillWagner

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 use readonly record struct
    • Instead of a static property with a listing of students, maybe we could do something else?

In looking at this, it does seem like the op has a valid point. Why show three group clauses when two would work.

IEvangelist avatar Jul 29 '22 17:07 IEvangelist

Also fixed in #39194

BillWagner avatar Feb 20 '24 21:02 BillWagner