我試圖獲得一門課程中獲得最高分數的學生列表。 我有一個學生類和課程類,如下所示: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查詢是什麼樣的?
你有沒有做過關於'.MAX'函數的任何研究或搜索功能,你能告訴我們你迄今爲止嘗試過的東西嗎? 'SO不是'代碼工廠網站',請有意識地努力/嘗試自己嘗試一下,然後回報如果您有任何問題 – MethodMan
@MethodMan好吧,至少OP會竭盡全力提供樣本數據。第一個問題很好。一個解決方案的第一個鏡頭就會使上升幅度猛增。太糟糕了,錯過了機會。 –
@GertArnold發表的代碼是好的我同意..但谷歌搜索Linq Max的例子可能會節省OP在我看來很多時間和麻煩。至少我沒有downvote它.. – MethodMan