我有一个看起来像这样的对象:
public class Student
{
public string Name { get; set; }
public int Grade { get; set; }
}
我想创建以下查询:按学生姓名分组成绩,按成绩对每个学生组进行排序,并按每组中的最高成绩订购组。
所以它看起来像这样:
A 100
A 80
B 80
B 50
B 40
C 70
C 30
我创建了以下查询:
StudentsGrades.GroupBy(student => student.Name)
.OrderBy(studentGradesGroup => studentGradesGroup.Max(student => student.Grade));
但是返回IEnumerable
IGrouping
,我无法对列表进行排序,除非我在另一个foreach
查询中执行此操作,并使用{将结果添加到其他列表{1}}。
有更好的方法吗?
答案 0 :(得分:117)
不确定
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);
请注意,您可以在订购后取得每组中的一年级,因为您已经知道第一个参赛作品的成绩最高。
然后你可以用以下方式显示它们:
foreach (var group in query)
{
Console.WriteLine("Group: {0}", group.Name);
foreach (var student in group.Students)
{
Console.WriteLine(" {0}", student.Grade);
}
}
答案 1 :(得分:18)
没有投影的方法:
StudentsGrades.OrderBy(student => student.Name).
ThenBy(student => student.Grade);
答案 2 :(得分:15)
我认为您需要一个额外的投影,将每个组映射到该组的排序版本:
.Select(group => group.OrderByDescending(student => student.Grade))
看起来你可能希望在此之后再进行另一次展平操作,这会给你一系列学生而不是一系列的小组:
.SelectMany(group => group)
您可以随时将两者都折叠为一起执行投影和展平的单 SelectMany
调用。
编辑:
正如Jon Skeet指出的那样,整体查询存在一些效率低下的问题;从排序每个组中获得的信息不会用于组本身的排序。通过将每个组的排序移动到之前组本身的排序,Max
查询可以被转移到更简单的First
查询中。
答案 3 :(得分:4)
试试这个......
public class Student
{
public int Grade { get; set; }
public string Name { get; set; }
public override string ToString()
{
return string.Format("Name{0} : Grade{1}", Name, Grade);
}
}
class Program
{
static void Main(string[] args)
{
List<Student> listStudents = new List<Student>();
listStudents.Add(new Student() { Grade = 10, Name = "Pedro" });
listStudents.Add(new Student() { Grade = 10, Name = "Luana" });
listStudents.Add(new Student() { Grade = 10, Name = "Maria" });
listStudents.Add(new Student() { Grade = 11, Name = "Mario" });
listStudents.Add(new Student() { Grade = 15, Name = "Mario" });
listStudents.Add(new Student() { Grade = 10, Name = "Bruno" });
listStudents.Add(new Student() { Grade = 10, Name = "Luana" });
listStudents.Add(new Student() { Grade = 11, Name = "Luana" });
listStudents.Add(new Student() { Grade = 22, Name = "Maria" });
listStudents.Add(new Student() { Grade = 55, Name = "Bruno" });
listStudents.Add(new Student() { Grade = 77, Name = "Maria" });
listStudents.Add(new Student() { Grade = 66, Name = "Maria" });
listStudents.Add(new Student() { Grade = 88, Name = "Bruno" });
listStudents.Add(new Student() { Grade = 42, Name = "Pedro" });
listStudents.Add(new Student() { Grade = 33, Name = "Bruno" });
listStudents.Add(new Student() { Grade = 33, Name = "Luciana" });
listStudents.Add(new Student() { Grade = 17, Name = "Maria" });
listStudents.Add(new Student() { Grade = 25, Name = "Luana" });
listStudents.Add(new Student() { Grade = 25, Name = "Pedro" });
listStudents.GroupBy(g => g.Name).OrderBy(g => g.Key).SelectMany(g => g.OrderByDescending(x => x.Grade)).ToList().ForEach(x => Console.WriteLine(x.ToString()));
}
}
答案 4 :(得分:1)
或者你可以这样做:
var _items = from a in StudentsGrades
group a by a.Name;
foreach (var _itemGroup in _items)
{
foreach (var _item in _itemGroup.OrderBy(a=>a.grade))
{
------------------------
--------------------------
}
}