2012-09-11 133 views
0

嘗試在NHibernate QueryOver中實現以下查詢。動態添加投影到NHibernate查詢

SQL

SELECT 
    SUM(GrossChargeAmount) 
FROM Charges 
INNER JOIN Account on Account.Chargekey = Charges.Chargekey 

SELECT 
    SUM(GrossChargeAmount), 
    Account.AccountHolder 
FROM Charges 
INNER JOIN Account on Account.Chargekey = Charges.Chargekey 
GROUP BY 
    Account.AccountHolder 

NHibernate的:

var accountAlias = null; 

var baseQuery = session.QueryOver<Charges>() 
       .JoinAlias(c => c.Account,() => accountAlias) 
       .SelectList(s => s.SelectSum(c => c.GrossChargeAmount)) 

我有一個構建baseQuery代碼。我然後根據方法的參數將要追加GROUP BY到像baseQuery:

baseQuery.SelectList(s => s.GroupBy(() => accountAlias.AccountHolder) 

然而,這結束了復位我baseQuery預測,基本上「overwritting」的初步預測(.SelectList(S => s.SelectSum(c => c.GrossChargeAmount)))。在這個階段,查詢的執行不會有任何SUM()bu只是GROUP BY。

TLDR;如何將投影添加到Nhibernate QueryOver實例?

最後,但並非最不重要,我NHibernate的運行2.0.50727

+0

有沒有例如NHibernate 2.0.50727 –

回答

0

嘗試:

baseQuery.SelectList(s => 
    s.SelectSum(c => c.GrossChargeAmount) 
    .GroupBy(() => accountAlias.AccountHolder)) 

你也可以建立在傳遞給的SelectList表達,如果你喜歡

+0

最後,我最終維護了一個IList ,我將它傳遞給了baseQuery.Select()。謝謝! – Alex