0

这很令人费解;生成的 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显然测量员工的平均时间只是为了举例。我真正的查询是针对其他实体..

4

1 回答 1

1

好吧,我完全傻了。忘记Alias()Avg投影中添加一个... duhh

于 2011-06-09T07:04:03.753 回答