I have an object that looks something like this:
public class Student
{
public string Name { get; set; }
public int Grade { get; set; }
}
I would like to create the following query: group grades by student name, order each student group by grades, and order groups by max grade in each group.
So the result should look like this:
John 100
John 80
Lucy 80
Lucy 50
Lucy 40
Eric 70
Eric 30
I created the following query:
StudentsGrades.GroupBy(student => student.Name)
.OrderBy(studentGradesGroup => studentGradesGroup.Max(student => student.Grade));
But that returns IEnumerable
IGrouping
, and I have no way to sort the list inside, unless I do that in another foreach
query and add the results to a different list using AddRange
.
Is there a prettier way to do that?
Sure:
var query = grades.GroupBy(student => student.Name)
.Select(group =>
new { Name = group.Key,
Students = group.OrderByDescending(x => x.Grade) })
.OrderByDescending(group => group.Students.First().Grade);
Note that you can get away with just taking the first grade within each group after ordering, because you already know the first entry will have the highest grade.
Then you could display them with:
foreach (var group in query)
{
Console.WriteLine("Group: {0}", group.Name);
foreach (var student in group.Students)
{
Console.WriteLine(" {0}", student.Grade);
}
}