2016-01-21 76 views
-3

我試圖獲得一門課程中獲得最高分數的學生列表。 我有一個學生類和課程類,如下所示:LINQ查詢在每門課程中獲得頂尖學生(得分最高)

public class Student 
{ 
    public string First { get; set; } 
    public string Last { get; set; } 
    public int ID { get; set; } 
    public Level Year { get; set; } 
    public List<Course> Courses; 
} 

public class Course 
{ 
    public string Name { get; set; } 
    public int Score { get; set; } 
} 

學生類有課程表。每門課程都有名稱和分數。

這裏是數據源

public List<Student> students = new List<Student> 
    { 
     new Student {First="Svetlana", Last="Omelchenko", ID=111, Year = Level.First, 
      Courses = new List<Course> { 
       new Course { Name = "Maths", Score = 97 }, 
       new Course { Name = "Science", Score = 92}, 
       new Course { Name = "English", Score = 81}, 
       new Course { Name = "Arts", Score = 60}}}, 
     new Student {First="Claire", Last="O'Donnell", ID=112, Year = Level.First, 
      Courses = new List<Course> { 
       new Course { Name = "Maths", Score = 75 }, 
       new Course { Name = "Science", Score = 84}, 
       new Course { Name = "English", Score = 91}, 
       new Course { Name = "Arts", Score = 39}}}, 
     new Student {First="Sven", Last="Mortensen", ID=113, Year = Level.First, 
      Courses = new List<Course> { 
       new Course { Name = "Maths", Score = 88 }, 
       new Course { Name = "Science", Score = 94}, 
       new Course { Name = "English", Score = 65}, 
       new Course { Name = "Arts", Score = 91}}}, 
     new Student {First="Cesar", Last="Garcia", ID=114, Year = Level.First, 
      Courses = new List<Course> { 
       new Course { Name = "Maths", Score = 97 }, 
       new Course { Name = "Science", Score = 89}, 
       new Course { Name = "English", Score = 85}, 
       new Course { Name = "Arts", Score = 82}}}, 
     new Student {First="Debra", Last="Garcia", ID=115, Year = Level.First, 
      Courses = new List<Course> { 
       new Course { Name = "Maths", Score = 35 }, 
       new Course { Name = "Science", Score = 72}, 
       new Course { Name = "English", Score = 92}, 
       new Course { Name = "Arts", Score = 70}}}, 
     new Student {First="Fadi", Last="Fakhouri", ID=116, Year = Level.First, 
      Courses = new List<Course> { 
       new Course { Name = "Maths", Score = 99 }, 
       new Course { Name = "Science", Score = 86}, 
       new Course { Name = "English", Score = 90}, 
       new Course { Name = "Arts", Score = 94}}}, 
     new Student {First="Hanying", Last="Feng", ID=117, Year = Level.First, 
      Courses = new List<Course> { 
       new Course { Name = "Maths", Score = 93 }, 
       new Course { Name = "Science", Score = 92}, 
       new Course { Name = "English", Score = 80}, 
       new Course { Name = "Arts", Score = 87}}}, 
     new Student {First="Hugo", Last="Garcia", ID=118, Year = Level.First, 
      Courses = new List<Course> { 
       new Course { Name = "Maths", Score = 92 }, 
       new Course { Name = "Science", Score = 90}, 
       new Course { Name = "English", Score = 83}, 
       new Course { Name = "Arts", Score = 78}}}, 
     new Student {First="Lance", Last="Tucker", ID=119, Year = Level.First, 
      Courses = new List<Course> { 
       new Course { Name = "Maths", Score = 68 }, 
       new Course { Name = "Science", Score = 79}, 
       new Course { Name = "English", Score = 88}, 
       new Course { Name = "Arts", Score = 92}}}, 
     new Student {First="Terry", Last="Adams", ID=120, Year = Level.First, 
      Courses = new List<Course> { 
       new Course { Name = "Maths", Score = 99 }, 
       new Course { Name = "Science", Score = 82}, 
       new Course { Name = "English", Score = 81}, 
       new Course { Name = "Arts", Score = 79}}}, 
     new Student {First="Eugene", Last="Zabokritski", ID=121, Year = Level.First, 
      Courses = new List<Course> { 
       new Course { Name = "Maths", Score = 96 }, 
       new Course { Name = "Science", Score = 85}, 
       new Course { Name = "English", Score = 91}, 
       new Course { Name = "Arts", Score = 60}}}, 
     new Student {First="Michael", Last="Tucker", ID=122, Year = Level.First, 
      Courses = new List<Course> { 
       new Course { Name = "Maths", Score = 94 }, 
       new Course { Name = "Science", Score = 92}, 
       new Course { Name = "English", Score = 91}, 
       new Course { Name = "Arts", Score = 91}}} 
    }; 

我想如下得到一個輸出:

Math: 
    Fadi Fakhouri- 99 
Science: 
    Svetlana Omelchenko - 92 
    Michael Tucker - 92 
English: 
    Debra Garcia - 92 
Arts: 
    Fadi Fakhouri - 94 

應我的LINQ查詢是什麼樣的?

+7

你有沒有做過關於'.MAX'函數的任何研究或搜索功能,你能告訴我們你迄今爲止嘗試過的東西嗎? 'SO不是'代碼工廠網站',請有意識地努力/嘗試自己嘗試一下,然後回報如果您有任何問題 – MethodMan

+0

@MethodMan好吧,至少OP會竭盡全力提供樣本數據。第一個問題很好。一個解決方案的第一個鏡頭就會使上升幅度猛增。太糟糕了,錯過了機會。 –

+1

@GertArnold發表的代碼是好的我同意..但谷歌搜索Linq Max的例子可能會節省OP在我看來很多時間和麻煩。至少我沒有downvote它.. – MethodMan

回答

0

下面是做

var allCourses = students.SelectMany(s => s.Courses); 
var groupedByCourse = allCourses.GroupBy(c => c.Name); 
foreach (var courseGroup in groupedByCourse) 
{ 
    Console.WriteLine(courseGroup.Key + ":"); 
    int maxMark = courseGroup.Max(g => g.Score); 
    var studentsWithMaxMark = students.Where(s => s.Courses.Any(c => c.Name == courseGroup.Key && c.Score == maxMark)); 
    foreach (var student in studentsWithMaxMark) 
    { 
     Console.WriteLine("\t" + student.First + " " + student.Last + " - " + maxMark); 
    } 
} 

輸出的途徑之一以下:

Maths: 
    Fadi Fakhouri - 99 
    Terry Adams - 99 
Science: 
    Sven Mortensen - 94 
English: 
    Debra Garcia - 92 
Arts: 
    Fadi Fakhouri - 94 

以上IMO是明確的,相當的可讀性。你可以在一個LINQ查詢中完成整個事情。

+0

這是另一種方式: –

0

這是另一種使用Linq的方法。

var query = from student in db.students 
        from course in student.Courses 
        group course by course.Name into g 
        select new 
        { 
         CourseName = g.Key, 
         Marks = g.Max(c => c.Score), 
         studentGroup = (
          from student in db.students 
          let maxMark = g.Max(c => c.Score) 
          where student.Courses.Any(c => c.Name == g.Key && c.Score == maxMark) 
          select student 
         ) 
        }; 

     foreach(var group in query) 
     { 
      Console.WriteLine("Course: {0}", group.CourseName); 
      foreach (var student in group.studentGroup) 
      { 
       Console.WriteLine("\t{0} {1} : {2}", student.First, student.Last, group.Marks); 
      } 
     }