NHibernate中有任何算术运算预测吗?

And*_*vko 6 c# math nhibernate nhibernate-projections queryover

我想从NHibernate获取这个SQL:

SELECT SUM(color_pages) * SUM(total_pages)
FROM connector_log_entry
GROUP BY department_name
Run Code Online (Sandbox Code Playgroud)

但我无法在任何地方找到任何算术运算(*)投影.

这是我到目前为止的代码:

Session.QueryOver<ConnectorLogEntry>()
       .SelectList(list => list
           .SelectGroup(m => m.DepartmentName)
           .WithAlias(() => dto.Department)
           .Select(Projections.Sum<ConnectorLogEntry>(m => m.TotalPages))
           //.Select(Projections.Sum<ConnectorLogEntry>(m => m.ColorPages))
           .WithAlias(() => dto.TotalColorPercentage))
       .TransformUsing(Transformers.AliasToBean<DepartmentConsumption>());
Run Code Online (Sandbox Code Playgroud)

Nat*_*lch 8

算术运算符可以通过VarArgsSQLFunctionSQL函数用于条件查询.在您的特定情况下,这看起来像:

Session.QueryOver<ConnectorLogEntry>()
    .SelectList(list =>
        list.SelectGroup(m => m.DepartmentName)
            .WithAlias(() => dto.Department)
            .Select(Projections.SqlFunction(
                new VarArgsSQLFunction("(", "*", ")"),
                NHibernateUtil.Int32,
                Projections.Sum<ConnectorLogEntry>(m => m.TotalPages),
                Projections.Sum<ConnectorLogEntry>(m => m.ColorPages)))
            .WithAlias(() => dto.TotalColorPercentage))
    .TransformUsing(Transformers.AliasToBean<DepartmentConsumption>());
Run Code Online (Sandbox Code Playgroud)

此技术将字符串直接注入生成的SQL中,因此您需要确保底层数据库支持您使用的运算符.