NHibernate等效的QueryOver

时间:2012-12-04 14:59:31

标签: nhibernate queryover

对于为下面提到的SQL编写NHibernate QueryOver等价物,我将不胜感激:

select sum(s.StudentCount),sum(w.TotalEarningsAmount),
avg(w.TotalEarningsAmount),count(w.TotalEarningsAmount) 
from School s inner join s.Earnings w where s.Active = 1"

1 个答案:

答案 0 :(得分:3)

Earning w = null; //alias variable
Dto dto = null; //make a dto object

var dtoList = Session.QueryOver<School>()
    .JoinAlias(x => x.Earnings, () => w)
    .Where(x => x.isActive)
    .SelectList(list => list
        .SelectSum(x => x.StudentCount).WithAlias(() => dto.StudentCountSum)
        .SelectSum(() => w.TotalEarningsAmount).WithAlias(() => dto.TotalEarningsAmountSum)
        .SelectAvg(() => w.TotalEarningsAmount).WithAlias(() => dto.TotalEarningsAmountAvg)
        .SelectCount(() => w.TotalEarningsAmount).WithAlias(() => dto.TotalEarningsAmountCount))
    .TransformUsing(Transformers.AlaisToBean<Dto>())
    .List<Dto>();