4

我的项目中有两个类,如下所示:

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
4

1 回答 1

7
            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;}
}
于 2012-05-16T10:07:57.487 回答