0

Trying to implement following queries in 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))

I have code that constructs the baseQuery. I then, depending on method parameters will want to append the GROUP BY to the baseQuery like:

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

This however ends up resetting projections on my baseQuery and essentially "overwritting" the initial projections ( .SelectList(s => s.SelectSum(c => c.GrossChargeAmount)) ). At this stage an execution of the query will not have any SUM() bu just the GROUP BY.

TLDR; How do I add projections to an Nhibernate QueryOver instance?

Last, but not least I'm running on NHibernate 2.0.50727

Alex
  • 674
  • 6
  • 18

1 Answers1

0

try:

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

you could also build up the expression passed in to SelectList if you prefer

Martin Ernst
  • 5,629
  • 2
  • 17
  • 14
  • In the end, I ended up maintaing an IList that I pass into baseQuery.Select(). Thanks! – Alex Sep 19 '12 at 00:17