获得所有5人以上的评分教室
本文关键字:教室 5人 | 更新日期: 2023-09-27 17:50:48
我试图想出Linq为一个特殊的情况。我不想使用我客户的数据库表的名称,所以我把情况翻译成典型的教室设置。
一个教室有很多学生,甚至没有学生。每个班级可能会或可能不会被分配一个平均成绩。不用担心分数从何而来,也不用担心为什么分数会和课程分开。
使用Linq,我想得到所有学生人数超过5人且平均成绩的教室。
对象看起来像这样。
class Classrooms {
Guid ClassroomId;
string subject;
}
class Students {
Guid Student;
Guid ClassroomId;
}
class Score {
Guid ClassroomId;
int someScoreHere;
}
在SQL中,我寻找这样的东西:
SELECT COUNT(*) As NumberOfStudents, ClassroomId
FROM Students WHERE ClassroomId IN (SELECT ClassroomId FROM Score)
GROUP BY ClassroomId HAVING COUNT(StudentId) > 5 BY NumberOfStudents;
我的失败Linq尝试获得教室列表;
Guid[] scores = (from score in db.Score select score.ClassroomId).ToArray();
List<Classroom> result = (from c in db.Classroom
where db.Student.Contains(c.ClassroomId))
group c by c.ClassroomId.... BLEEEH
from c in db.Classroom
where db.Score.Select(s => s.ClassroomId).Contains(c.ClassroomId)
group c by c.ClassroomId into g
where g.Count() > 5
select new { NumberOfStudents = g.Count(), ClassroomId = g.Key }