如何在NHibernate中使用SqlFunction和VarArgsSQLFunction进行分组

时间:2012-10-16 17:08:04

标签: sql-server-2008 nhibernate

我正在尝试使用QueryOver实现以下SQL查询:

SELECT [Time]/1000
FROM TableName
GROUP BY [Time]/1000

这是我目前的尝试:

var result = session
    .QueryOver<TableName>
    .Select(Projections.GroupProperty(
        Projections.SqlFunction(
            new VarArgsSQLFunction("(", "/", ")"),
            NHibernateUtil.Int64,
            Projections.Property("Time")
            Projections.Constant(1000))
    ))
    .List<object>();

不幸的是我得到以下异常(GenericADOException):

could execute query
[ SELECT (this_.Time/@p0) as y0_ FROM [TableName] this_ GROUP BY (this_.Time/?) ]

内在的例外:

Incorrect syntax near ?.

我可以用“Sum”替换“GroupProperty”并且它可以工作。知道缺少什么吗?


更新: 显然这是NHibernate中的一个错误。另请参阅this question

1 个答案:

答案 0 :(得分:1)

为什么不使用Projections.SqlGroupProjection:

var result = session
    .QueryOver<TableName>
    .Select(Projections.SqlGroupProjection(
                        Time/1000 AS TimeValue", 
                        "Time/1000", 
                        new[]{"TimeValue"}, 
                        new[]{NHibernateUtil.Int32}))
    .List<object>();