<p>For example, I want to populate a gridview control in an ASP.NET web page with only the data necessary for the # of rows displayed. How can NHibernate support this?</p>

[{'answer_id': 54773, 'author': 'NotMyself', 'author_id': 303, 'author_profile': 'https://Stackoverflow.com/users/303', 'pm_score': 5, 'selected': False, 'text': '<p>How about using Linq to NHibernate as discussed in <a href="http://www.ayende.com/Blog/archive/2007/03/20/Linq-For-NHibernate-Orderring-and-Paging.aspx" rel="noreferrer">this blog post</a> by Ayende?</p>\n\n<p>Code Sample:</p>\n\n<pre><code>(from c in nwnd.Customers select c.CustomerID)\n        .Skip(10).Take(10).ToList(); \n</code></pre>\n\n<p>And here is a detailed post by the NHibernate team blog on <a href="http://www.nhforge.org/blogs/nhibernate/archive/2008/08/31/data-access-with-nhibernate.aspx" rel="noreferrer">Data Access With NHibernate</a> including implementing paging.</p>\n'}, {'answer_id': 54777, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 8, 'selected': True, 'text': '<p><code>ICriteria</code> has a <code>SetFirstResult(int i)</code> method, which indicates the index of the first item that you wish to get (basically the first data row in your page). </p>\n\n<p>It also has a <code>SetMaxResults(int i)</code> method, which indicates the number of rows you wish to get (i.e., your page size).</p>\n\n<p>For example, this criteria object gets the first 10 results of your data grid:</p>\n\n<pre><code>criteria.SetFirstResult(0).SetMaxResults(10);\n</code></pre>\n'}, {'answer_id': 54858, 'author': 'Marcio Aguiar', 'author_id': 4213, 'author_profile': 'https://Stackoverflow.com/users/4213', 'pm_score': 3, 'selected': False, 'text': '<p>I suggest that you create a specific structure to deal with pagination. Something like (I\'m a Java programmer, but that should be easy to map):</p>\n\n<pre><code>public class Page {\n\n   private List results;\n   private int pageSize;\n   private int page;\n\n   public Page(Query query, int page, int pageSize) {\n\n       this.page = page;\n       this.pageSize = pageSize;\n       results = query.setFirstResult(page * pageSize)\n           .setMaxResults(pageSize+1)\n           .list();\n\n   }\n\n   public List getNextPage()\n\n   public List getPreviousPage()\n\n   public int getPageCount()\n\n   public int getCurrentPage()\n\n   public void setPageSize()\n\n}\n</code></pre>\n\n<p>I didn\'t supply an implementation, but you could use the methods suggested by <a href="https://stackoverflow.com/questions/54754/how-can-you-do-paging-with-nhibernate#54777">@Jon</a>. Here\'s a <a href="http://in.relation.to/Bloggers/PaginationInHibernateAndEJB3" rel="nofollow noreferrer">good discussion</a> for you to take a look.</p>\n'}, {'answer_id': 138024, 'author': 'zadam', 'author_id': 410357, 'author_profile': 'https://Stackoverflow.com/users/410357', 'pm_score': 4, 'selected': False, 'text': '<p>Most likely in a GridView you will want to show a slice of data plus the total number of rows (rowcount) of the total amount of data that matched your query.</p>\n\n<p>You should use a MultiQuery to send both the Select count(*) query and .SetFirstResult(n).SetMaxResult(m) queries to your database in a single call.</p>\n\n<p>Note the result will be a list that holds 2 lists, one for the data slice and one for the count.</p>\n\n<p>Example:</p>\n\n<pre><code>IMultiQuery multiQuery = s.CreateMultiQuery()\n    .Add(s.CreateQuery("from Item i where i.Id &gt; ?")\n            .SetInt32(0, 50).SetFirstResult(10))\n    .Add(s.CreateQuery("select count(*) from Item i where i.Id &gt; ?")\n            .SetInt32(0, 50));\nIList results = multiQuery.List();\nIList items = (IList)results[0];\nlong count = (long)((IList)results[1])[0];\n</code></pre>\n'}, {'answer_id': 433210, 'author': 'Barbaros Alp', 'author_id': 51734, 'author_profile': 'https://Stackoverflow.com/users/51734', 'pm_score': 5, 'selected': False, 'text': '<pre><code>public IList&lt;Customer&gt; GetPagedData(int page, int pageSize, out long count)\n        {\n            try\n            {\n                var all = new List&lt;Customer&gt;();\n\n                ISession s = NHibernateHttpModule.CurrentSession;\n                IList results = s.CreateMultiCriteria()\n                                    .Add(s.CreateCriteria(typeof(Customer)).SetFirstResult(page * pageSize).SetMaxResults(pageSize))\n                                    .Add(s.CreateCriteria(typeof(Customer)).SetProjection(Projections.RowCountInt64()))\n                                    .List();\n\n                foreach (var o in (IList)results[0])\n                    all.Add((Customer)o);\n\n                count = (long)((IList)results[1])[0];\n                return all;\n            }\n            catch (Exception ex) { throw new Exception("GetPagedData Customer da hata", ex); }\n      }\n</code></pre>\n\n<p>When paging data is there another way to get typed result from MultiCriteria or everyone does the same just like me ?</p>\n\n<p>Thanks</p>\n'}, {'answer_id': 1329082, 'author': 'Jeremy D', 'author_id': 2096983, 'author_profile': 'https://Stackoverflow.com/users/2096983', 'pm_score': 6, 'selected': False, 'text': '<p>You can also take advantage of the Futures feature in NHibernate to execute the query to get the total record count as well as the actual results in a single query.</p>\n\n<p><strong>Example</strong></p>\n\n<pre><code> // Get the total row count in the database.\nvar rowCount = this.Session.CreateCriteria(typeof(EventLogEntry))\n    .Add(Expression.Between("Timestamp", startDate, endDate))\n    .SetProjection(Projections.RowCount()).FutureValue&lt;Int32&gt;();\n\n// Get the actual log entries, respecting the paging.\nvar results = this.Session.CreateCriteria(typeof(EventLogEntry))\n    .Add(Expression.Between("Timestamp", startDate, endDate))\n    .SetFirstResult(pageIndex * pageSize)\n    .SetMaxResults(pageSize)\n    .Future&lt;EventLogEntry&gt;();\n</code></pre>\n\n<p>To get the total record count, you do the following:</p>\n\n<pre><code>int iRowCount = rowCount.Value;\n</code></pre>\n\n<p>A good discussion of what Futures give you is <a href="http://ayende.com/Blog/archive/2009/04/27/nhibernate-futures.aspx" rel="noreferrer">here</a>.</p>\n'}, {'answer_id': 5073510, 'author': 'Leandro de los Santos', 'author_id': 627627, 'author_profile': 'https://Stackoverflow.com/users/627627', 'pm_score': 6, 'selected': False, 'text': '<p>From NHibernate 3 and above, you can use <code>QueryOver&lt;T&gt;</code>:</p>\n\n<pre><code>var pageRecords = nhSession.QueryOver&lt;TEntity&gt;()\n            .Skip((PageNumber - 1) * PageSize)\n            .Take(PageSize)\n            .List();\n</code></pre>\n\n<p>You may also want to explicitly order your results like this:</p>\n\n<pre><code>var pageRecords = nhSession.QueryOver&lt;TEntity&gt;()\n            .OrderBy(t =&gt; t.AnOrderFieldLikeDate).Desc\n            .Skip((PageNumber - 1) * PageSize)\n            .Take(PageSize)\n            .List();\n</code></pre>\n'}, {'answer_id': 61224560, 'author': 'Marcin', 'author_id': 8537786, 'author_profile': 'https://Stackoverflow.com/users/8537786', 'pm_score': 0, 'selected': False, 'text': "<p>You don't need to define 2 criterias, you can define one and clone it. \nTo clone nHibernate criteria you can  use a simple code:</p>\n\n<pre><code>var criteria = ... (your criteria initializations)...;\nvar countCrit = (ICriteria)criteria.Clone();\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/54754', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4872/']