开发者

projection of type average(datediff) always returns 0.0

开发者 https://www.devze.com 2023-03-10 18:48 出处:网络
This is very puzzling; the generated SQL is perfectly good, and I get correct results when running it manually.

This is very puzzling; the generated SQL is perfectly good, and I get correct results when running it manually.

However, somewhere in the transformation process the 'AverageTime' field is set to 0.0 instead of the correct result.

my query:

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("开发者_如何学CDepartment.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; }
        }

the generated query is correct:

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. obviously measuring employees' average time is just for the example's sake. My real query is for other entities..


OK I'm totaly stupid. Forgot to add an Alias() to the Avg projection... duhh

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号