类型平均值(datediff)的投影总是返回0.0

时间:2011-06-07 11:25:09

标签: nhibernate nhibernate-projections

这非常令人费解;生成的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;

P.S。显然,衡量员工的平均时间只是为了举例。我真正的查询是针对其他实体..

1 个答案:

答案 0 :(得分:1)

好吧,我完全是傻瓜。忘了将Alias()添加到Avg投影... duh