2013-04-21 139 views
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 } }, 

    }; 

如何可以根據自己的分數avarages我一羣學生?

+5

那麼你嘗試過什麼?你想要什麼樣的平均水平? – 2013-04-21 19:54:54

回答

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); 
    } 
}