获得超过5名学生的所有得分教室

时间:2015-05-25 16:19:42

标签: c# linq entity-framework sql-to-linq-conversion

我一直试图为特定案件提出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

1 个答案:

答案 0 :(得分:1)

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 }