动态添加投影到NHibernate查询

时间:2012-09-11 15:02:58

标签: c# nhibernate orm queryover

尝试在NHibernate QueryOver中实现以下查询。

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上的投影并基本上“覆盖”初始投影(.SelectList(s =&gt; s.SelectSum(c =&gt; c.GrossChargeAmount)))。在这个阶段,查询的执行将不会有任何SUM()只是GROUP BY。

TLDR; 如何向Nhibernate QueryOver实例添加投影?

最后,但并非最不重要的是我正在运行NHibernate 2.0.50727

1 个答案:

答案 0 :(得分:0)

尝试:

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

如果您愿意,也可以构建传递给SelectList的表达式