title: NHibernate Paging performance (Better option)
body: <p>In that simple case:</p>
  
  <pre><code>public class Person
  {
       public int Id {get;set;}
       public int Name {get;set;}
  }
  </code></pre>
  
  <ul>
  <li>I need generate a Grid with paging and ordering</li>
  <li>My database has about 100k persons</li>
  </ul>
  
  <p><strong>What option has better performance :</strong></p>
  
  <p><strong>1)</strong> Get all elemtents in the first time and after take advantage of NHibernate First Level Cache, example:</p>
  
  <pre><code>personRep.FindAll().OrderBy(s =&gt;s.Name).AsPagination(pageNumber,pageSize);
  </code></pre>
  
  <p>Obs.: AsPagination is a extension method...</p>
  
  <p><strong>2)</strong> Get only the actual page from database, example:    </p>
  
  <pre><code>   public virtual IList&lt;T&gt; GetPaginedList(int __pageIndex, int __pageSize,out int __total)
      {            
          var _rowCount = Session.CreateCriteria(typeof(T))
              .SetProjection(Projections.RowCount()).FutureValue&lt;Int32&gt;();
  
          var _results = Session.CreateCriteria(typeof(T))
              .SetFirstResult(__pageIndex * __pageSize)
              .SetMaxResults(__pageSize)
              .Future&lt;T&gt;();
  
          __total = _rowCount.Value; 
          return _results;
      } 
  </code></pre>