C# NHibernate将sql查询转换为NHibernate查询覆盖查询

C# NHibernate将sql查询转换为NHibernate查询覆盖查询,c#,sql,nhibernate,C#,Sql,Nhibernate,所以我有一点SQL查询,我不知道如何转换为NHibernate语法 cast(case when count(distinct order) > 1 then count(distinct order) * -1 else max(order.orderId) end as int) 我目前拥有以下NHibernate代码: projectionList.Add( Projections.Conditional( Restrictions.Gt(Projectio

所以我有一点SQL查询,我不知道如何转换为NHibernate语法

cast(case when count(distinct order) > 1 then count(distinct order) * -1 else max(order.orderId) end as int)
我目前拥有以下NHibernate代码:

projectionList.Add(
    Projections.Conditional(
        Restrictions.Gt(Projections.CountDistinct(() => orderDto), 1),
            Projections.CountDistinct(() => orderDto), // TODO: * -1
            Projections.Max(() => orderDto.orderId)
    )
);

如您所见,我不确定如何执行
*-1
部分?有人知道怎么做吗?

您可以使用
SQLFunctionTemplate
Projections.SqlFunction
来表示任何需要投影参数的复杂SQL。在您的情况下,您可以这样做:

    //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
        );