これは非常に不可解です。生成された SQL はまったく問題なく、手動で実行すると正しい結果が得られます。
ただし、変換プロセスのどこかで、「AverageTime」フィールドが正しい結果ではなく 0.0 に設定されます。
私の質問:
var query = Session.CreateCriteria<Employee>()
.Add(Expression.In("Department", departments.ToArray())) // departmentsContains(employee))
.Add(Expression.Ge("TimeOut", startTime)) // TimeOut >= startTime
.Add(Expression.Le("TimeOut", endTime)) // TimeOut <= endTime
.SetProjection(Projections.Alias(Projections.GroupProperty("Department.Id"), "Id")
, Projections.Alias(Projections.Count("Id"), "EmpCount") //total emps
, Projections.Avg( //average of..
Projections.SqlProjection("datediff(ss, {alias}.TimeIn ,{alias}.TimeOut) as AverageTime", new[] { "AverageTime" }, new[] { NHibernateUtil.Double }) // waiting time
)
)
.SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean<EmpsForStatistics>())
.List<EmpsForStatistics>();
private class EmpsForStatistics
{
public int DepartmentId { get; set; }
public int EmpCount { get; set; }
public double AverageTime { get; set; }
}
生成されたクエリは正しいです:
SELECT this_.Department_id as y0_, count(this_.Id) as y1_, avg(cast(datediff(ss, this_.TimeOut ,this_.TimeIn) as DOUBLE PRECISION)) as y2_
FROM nHibernate_test.dbo.[Employees] this_
WHERE this_.Department_id in (4004, 4005, 4006)
and this_.TimeOut >= '06/07/2011 08:27:58' and this_.TimeOut <= '06/07/2011 11:27:58'
GROUP BY this_.Department_id;
ps明らかに従業員の平均時間を測定するのは、例のためだけです。私の本当のクエリは、他のエンティティです..