我的项目中有两个类,如下所示:
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
答案 0 :(得分:6)
MyDTO myDTO = null;
Student studentAlias = null;
Teacher teacherAlias = null;
var results = _session.QueryOver<Teacher>(()=>teacherAlias)
.JoinAlias(x=>x.Students,()=>studentAlias, JoinType.LeftOuterJoin)
.SelectList(list => list
.SelectGroup(m => m).WithAlias(() => myDTO.Teacher)
.SelectCount(m => studentAlias.Id).WithAlias(() => myDTO.Count)
)
.TransformUsing(Transformers.AliasToBean<MyDTO>())
.List<MyDTO>();
DTO:
public class MyDTO{
public Teacher Teacher {get;set;} //or just name or whatever you need
public int Count {get;set;}
}