本文介绍了NHibernate 中是否有算术运算预测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从 NHibernate 获取这个 SQL:
I would like to get this SQL from NHibernate:
SELECT SUM(color_pages) * SUM(total_pages)
FROM connector_log_entry
GROUP BY department_name
但我在任何地方都找不到任何算术运算 (*) 投影.
But I can't find any arithmetic operation (*) projections anywhere.
这是我目前的代码:
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 函数在条件查询中使用算术运算符.在您的特定情况下,这看起来像:
Arithmetic operators can be used in criteria queries via the VarArgsSQLFunction
SQL function. In your particular case, this would look something like:
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,因此您需要确保底层数据库支持您使用的运算符.
This technique injects strings directly into the generated SQL, so you'll need to make sure the underlying database supports the operators you use.
这篇关于NHibernate 中是否有算术运算预测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!