How can you do paging with NHibernate?
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?
ICriteria
has a SetFirstResult(int i)
method, which indicates the index of the first item that you wish to get (basically the first data row in your page).
It also has a SetMaxResults(int i)
method, which indicates the number of rows you wish to get (ie, your page size).
For example, this criteria object gets the first 10 results of your data grid:
criteria.SetFirstResult(0).SetMaxResults(10);
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.
Example
// Get the total row count in the database.
var rowCount = this.Session.CreateCriteria(typeof(EventLogEntry))
.Add(Expression.Between("Timestamp", startDate, endDate))
.SetProjection(Projections.RowCount()).FutureValue<Int32>();
// Get the actual log entries, respecting the paging.
var results = this.Session.CreateCriteria(typeof(EventLogEntry))
.Add(Expression.Between("Timestamp", startDate, endDate))
.SetFirstResult(pageIndex * pageSize)
.SetMaxResults(pageSize)
.Future<EventLogEntry>();
To get the total record count, you do the following:
int iRowCount = rowCount.Value;
A good discussion of what Futures give you is here.
In NHibernate 3 you can use QueryOver
var pageRecords = nhSession.QueryOver<TEntity>()
.Skip(PageNumber * PageSize)
.Take(PageSize)
.List();
You also may want to explicitly order your results like this:
var pageRecords = nhSession.QueryOver<TEntity>()
.OrderBy(t => t.AnOrderFieldLikeDate).Desc
.Skip(PageNumber * PageSize)
.Take(PageSize)
.List();
链接地址: http://www.djcxy.com/p/20316.html
上一篇: 实体框架4与NHibernate
下一篇: 你如何使用NHibernate进行分页?