簡體   English   中英

NHibernate 將 sql 查詢轉換為 NHibernate QueryOver 查詢

[英]NHibernate transform sql query to NHibernate QueryOver query

所以我有一點 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部分? 有人知道怎么做嗎?

您可以使用SQLFunctionTemplateProjections.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
        ); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM