我的项目中有两个类,如下所示:
public class Teacher
{
public virtual long Id {get;set;}
public virtual IList<Student> Students {get;set;}
}
public class Student
{
public virtual long Id {get;set;}
public virtual string Name {get;set;}
}
我需要一个查询来获取每个教师的学生人数并根据计数结果进行分组。还与每个计数的频率(嗯...计数计数)。我不知道如何使用 queryover 来做到这一点。就像给定这样的数据:
teacher1.Students = [student1,student2,student3] //teacher1's student count is 3
teacher2.Students = [student4]
teacher3.Students = [student5,student6,student7]
teacher4.Students = [student8]
teacher5.Students = [] //empty
我需要这样的查询结果:
[
[0,1], //1 teacher has 0 students
[1,2],
[3,2] //2 teachers have 3 students
]
更新:sql是这样的:
select tc.tCount, count(tc.TRID)
from (select tr.Id as TRID, count(std.Id) as tCount
from Teacher tr
JOIN Student std on std.TeacherId = tr.Id
GROUP BY tr.Id ) as tc GROUP BY tc.tCount