求一个linq语句的写法

直接上代码的吧

public class Grade
{
public int Id { get; set; }
public List<Student> Students { get; set; }
}

public class Student
{
public int Id { get; set; }
public int GradeId { get; set; }
public int Score { get; set; }
}

public static void Test()
{
List<Grade> mockGrades = new List<Grade>() {
new Grade() { Id = 1, Students = new List<Student>(){ new Student(){ Id=1, GradeId=1, Score=99}, new Student(){ Id=2, GradeId=1, Score=100}, new Student(){ Id=3, GradeId=1, Score=88}}},
new Grade() { Id = 2, Students = new List<Student>(){ new Student(){ Id=4, GradeId=2, Score=21}, new Student(){ Id=5, GradeId=2, Score=35}, new Student(){ Id=6, GradeId=2, Score=24}}},
new Grade() { Id = 3, Students = new List<Student>(){ new Student(){ Id=7, GradeId=3, Score=97}, new Student(){ Id=8, GradeId=3, Score=71}, new Student(){ Id=9, GradeId=3, Score=99}}},
new Grade() { Id = 4, Students = new List<Student>(){ new Student(){ Id=10, GradeId=4, Score=90}, new Student(){ Id=11, GradeId=4, Score=69}, new Student(){ Id=12, GradeId=4, Score=32}}},
new Grade() { Id = 5, Students = new List<Student>(){ new Student(){ Id=13, GradeId=5, Score=37}, new Student(){ Id=14, GradeId=5, Score=84}, new Student(){ Id=15, GradeId=5, Score=56}}}
};

var result = from i in mockGrades
//let i.Students = top 2 students
//这里应该怎么写?我希望获得年级的列表并且i.Students属性即每个年级的学生列表只保留前2名
select i;
}

另外,由于用在entity framework里,似乎没办法在linq语句里new Grade()...

求助啊

海绵宝宝撒
浏览 547回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP