使用linq方法语法EF,使用GroupBy选择Max()

时间:2017-07-03 10:22:06

标签: asp.net entity-framework linq lambda delegates

我有一堆带有一些数据的类:

public class Teacher
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime bDate { get; set; }
    //One-to-one with course

    public int CourseId { get; set; }
    public virtual Course Course { get; set; }
}


public class Course
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection <Student_Course> Student_Courses { get; set; }
    public int TeacherId { get; set; }
    public virtual Teacher Teacher { get; set; }
}


public class Grade
{
    public int Id { get; set; }
    public int Mark { get; set; }
    //one-to-many with Student_Course

    public int Student_CourseId { get; set; }
    public Student_Course Student_Course { get; set; }
}

public class Student_Course
{
    public int Id { get; set; }
    //many-to-many with student
    public int StudentId { get; set; }
    public virtual Student Student { get; set; }

    //many-to-many with course
    public int CourseId { get; set; }
    public virtual Course Course { get; set; }
}

 public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime bDate { get; set; }
    //one-to-many with student_course
    public virtual ICollection <Student_Course> Student_Courses { get; set; }

}

现在我试着写一个查询 - 因为每个老师(姓名)需要按学生标记输出他最好的学生(姓名),以及最好的标记,从班级成绩,我的代码:

public IActionResult Task9()
    {
        var task9 = Db.Grades.GroupBy(gr => gr.Student_Course.Course.Teacher.Name).Select(x => new Task9
        {
            NameOfSt = x.Select(st => st.Student_Course.Student.Name).FirstOrDefault(),//Needs to correct this line
            NameOfTeac = x.Select(r => r.Student_Course.Course.Teacher.Name).FirstOrDefault(),
            BestMark = x.Max(gr => gr.Mark)


            //NameOfSt = Db.Students.FirstOrDefault(st => st.Id ==x.FirstOrDefault().Student_Course.StudentId).Name
        });
        return View(task9);
    }

这样代码就可以从学生那里输出正确的教师姓名和最佳标记。但学生的名字其实是错的。我该如何解决这个问题?谢谢你的帮助。

  

蒂雅克   BestMark   NameStud

2 个答案:

答案 0 :(得分:1)

GroupBy中,您需要使用分组的值(在本例中为x)来选择Grade最高的Grade对象。在下面的代码中,我通过bestGrade按降序排序获取Mark对象,然后获取第一个条目,然后从该对象获取学生,名称和最佳标记:

public IActionResult Task9()
{
    var task9 = Db.Grades.GroupBy(gr => gr.Student_Course.Course.Teacher.Name).Select(x =>
    {
        var bestGrade = x.OrderByDescending(y => y.Mark).First();
        return new Task9
        {
            NameOfSt = bestGrade.Student_Course.Student.Name,//Needs to correct this line
            NameOfTeac = bestGrade.Student_Course.Course.Teacher.Name,
            BestMark = bestGrade.Mark
        };
    });

    return View(task9);
}

答案 1 :(得分:1)

您可以在选择学生姓名前使用Mark的OrderByDescending:

var task9 = Db.Grades.GroupBy(gr => gr.Student_Course.Course.Teacher.Name).Select(x => new Task9
    {
        NameOfSt = x.OrderByDescending(st => st.Mark).Select(st => st.Student_Course.Student.Name).FirstOrDefault(),//Needs to correct this line
        NameOfTeac = x.Select(r => r.Student_Course.Course.Teacher.Name).FirstOrDefault(),
        BestMark = x.Max(gr => gr.Mark)        
    });