title: nHibernate Criteria queries produces duplicates using Paging
body: <p>Version nHibernate 2.1</p>
  
  <p>As can be seen from the vast array of similar questions - we're not alone in experiencing problems with paging generating duplicates.  We thought it was just happening with HQL queries but one of our clients has reported seeing it where the query is a Criteria query.</p>
  
  <p>So far we've only seen it on the reporting side - where we tend to collect bits of information from various 'associated' entities and use the AliasToBeanTransformer to put it into a DTO (DataTransferObject):</p>
  
  <p>.SetResultTransformer(new AliasToBeanResultTransformer(typeof(OurDTO)));</p>
  
  <p>We're not new to nHibernate, but we're certainly not aware of so many of the subtleties of it, and as a result weren't aware of</p>
  
  <p>new NHibernate.Transform.DistinctRootEntityResultTransformer()</p>
  
  <p>which could potentially eliminate our duplicates, but I'm struggling to see how we could do this when it's not a mapped entity, i.e. a DTO.</p>
  
  <p>We've tried creating a custom dialect which seems to have served some people well enough to be confident of consistent behaviour.</p>
  
  <p>I realise there's no such thing as a silver bullet and context is always the kicker, but has anyone managed to come up with a solution for this?</p>
  
  <p>The code we use to handle the collation of the pages is as follows:</p>
  
  <pre><code>        query.SetMaxResults(50);
  
          for (int i = 0; ; ++i)
          {
              query.SetFirstResult(i * 50);
              IList results = query.List();
              cumulativeResults.AddRange(results);
              OnRecordsLoaded(results.Count);
              if (results.Count &lt; 50)
              { break; }
  </code></pre>
  
  <p>Many thanks for any input on this.
  With kind regards
  Colin</p>