我想从 NHibernate 获取此 SQL:
SELECT SUM(color_pages) * SUM(total_pages)
FROM connector_log_entry
GROUP BY department_name
但我在任何地方都找不到任何算术运算(*)投影。
这是我到目前为止的代码:
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>());
算术运算符可以通过以下方式在条件查询中使用VarArgsSQLFunction
SQL 函数。在您的特定情况下,这看起来像:
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>());
此技术将字符串直接注入到生成的 SQL 中,因此您需要确保底层数据库支持您使用的运算符。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)