1

我有如下的学生名单

public class Student
{
    public string First { get; set; }
    public string Last { get; set; }
    public int ID { get; set; }
    public List<int> Scores;
}

List<Student> students = new List<Student>()
     {
         new Student { First = "a", Last = "aa", ID = 1, Scores = new List<int>() { 97, 92, 81, 60 } },
         new Student { First = "b", Last = "bb", ID = 2, Scores = new List<int>() { 75, 84, 91, 39 } },
         new Student { First = "c", Last = "cc", ID = 3, Scores = new List<int>() { 88, 94, 65, 91 } },

     };

如何根据学生的平均分数对他们进行分组?

4

1 回答 1

2
var groups = students.GroupBy(s => s.Scores.Average());

这为您提供了一个元素列表,其中键等于平均值​​,值等于具有该平均值的学生:

foreach(var g in groups){
    Console.WriteLine("Average: "+ g.Key);
    foreach(var s in g){
        Console.WriteLine(s.ID +":"+ s.First +" "+ s.Last);
    }
}
于 2013-04-21T20:02:39.763 回答