Linq order by, group by and order by each group?
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 it will look like this:
A 100
A 80
B 80
B 50
B 40
C 70
C 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) })
.OrderBy(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 be 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);
}
}
I think you want an additional projection that maps each group to a sorted-version of the group:
.Select(group => group.OrderByDescending(student => student.Grade))
It also appears like you might want another flattening operation after that which will give you a sequence of students instead of a sequence of groups:
.SelectMany(group => group)
You can always collapse both into a single SelectMany
call that does the projection and flattening together.
EDIT: As Jon Skeet points out, there are certain inefficiencies in the overall query; the information gained from sorting each group is not being used in the ordering of the groups themselves. By moving the sorting of each group to come before the ordering of the groups themselves, the Max
query can be dodged into a simpler First
query.
没有投影的方式:
StudentsGrades.OrderBy(student => student.Name).
ThenBy(student => student.Grade);
链接地址: http://www.djcxy.com/p/34272.html
下一篇: Linq排序,按组排列和排序?