[英]NHibernate transform sql query to NHibernate QueryOver query
So I have a bit of a SQL query which I am not sure how to convert to NHibernate syntax所以我有一点 SQL 查询,我不确定如何转换为 NHibernate 语法
cast(case when count(distinct order) > 1 then count(distinct order) * -1 else max(order.orderId) end as int)
I currently have the following NHibernate code:我目前有以下 NHibernate 代码:
projectionList.Add(
Projections.Conditional(
Restrictions.Gt(Projections.CountDistinct(() => orderDto), 1),
Projections.CountDistinct(() => orderDto), // TODO: * -1
Projections.Max(() => orderDto.orderId)
)
);
As you can see, I am not sure how to do the * -1
part?如您所见,我不确定如何执行* -1
部分? Has somebody any idea how to do it?有人知道怎么做吗?
You can use SQLFunctionTemplate
and Projections.SqlFunction
to express any complex SQL that needs projection parameters.您可以使用SQLFunctionTemplate
和Projections.SqlFunction
来表达任何需要投影参数的复杂 SQL。 In your case you can do something like this:在您的情况下,您可以执行以下操作:
//All projections parameters in template are replaced with placeholders like ?1 ?2 ?3...
//If possible move it to static field to avoid template parsing on each execution
var yourTemplate = new SQLFunctionTemplate(
NHibernateUtil.Int32, //Template result
"cast(case when ?1 > 1 then ?1 * -1 else ?2 end as int)");
//And in you query use the following projection:
Projections.SqlFunction(
yourTemplate,
null,
Projections.CountDistinct(() => orderDto), //?1 in template
Projections.Max(() => orderDto.orderId) //?2 in template
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.