Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate - How do I use a sum project on paged results
    text
    copied!<p>I'm trying to use paging in conjunction with a sum projection to get a sum of the values in a column for just the page of results I'm interested in. I'm using .NET, C# and NHibernate 3.1</p> <p>I have an ICriteria to start with which is related to all rows from the associated db table.</p> <p>I'm then doing the following to get a version with the first page (say, 10 items out of 40):</p> <pre><code>ICriteria recordsCriteria = CriteriaTransformer.Clone(criteria); recordsCriteria.SetFirstResult(0); recordsCriteria.SetMaxResults(10); </code></pre> <p>I'm using this ICriteria for something else so I then create two further clones:</p> <pre><code>ICriteria totalAggCriteria = CriteriaTransformer.Clone(criteria); ICriteria pageAggCriteria = CriteriaTransformer.Clone(recordsCriteria); </code></pre> <p>If I take a look inside these two new ones the first has 40 items in and the second has 10 - exactly what I want.</p> <p>Let's say the objects coming back from the DB have a column called "ColA" and it's of type <code>Int32</code>.</p> <p>From this, I want the sum of all 40 ColA values and the sum of the first 10 ColA values.</p> <p>To get the sum of all 40 ColA values, I do the following:</p> <pre><code>totalAggCriteria.SetProjection(NHibernate.Criterion.Projections.Sum("ColA")); var totalSum = totalAggCriteria.UniqueResult(); </code></pre> <p>The value in totalSum is correct.</p> <p>To get the sum of the first 10 ColA values, I'm trying the following:</p> <pre><code>pageAggCriteria.SetProjection(NHibernate.Criterion.Projections.Sum("ColA")); vat pageSum = pageAddCriteria.UniqueResult(); </code></pre> <p>However, this gives me the same value as the previous one - for all 40 ColA values.</p> <p>I've also tried the following but it gives the same result:</p> <pre><code>pageAggCriteria.SetProjection(NHibernate.Criterion.Projections.Sum(column)); pageAggCriteria.SetFirstResult(firstResult.Value); pageAggCriteria.SetMaxResults(pageSize.Value); pageSum = pageAggCriteria.UniqueResult(); </code></pre> <p>And also:</p> <pre><code>pageAggCriteria.SetFirstResult(firstResult.Value); pageAggCriteria.SetMaxResults(pageSize.Value); pageAggCriteria.SetProjection(NHibernate.Criterion.Projections.Sum(column)); pageSum = pageAggCriteria.UniqueResult(); </code></pre> <p>Can anyone give an idea on where I'm going wrong and how I can actually get the sum of the ColA values in the first 10 results?</p> <p>Thanks </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload