【发布时间】:2010-09-08 11:00:53
【问题描述】:
例如,我想在 ASP.NET 网页中填充一个 gridview 控件,其中仅包含显示的行数所需的数据。 NHibernate 如何支持这一点?
【问题讨论】:
标签: .net nhibernate orm pagination
例如,我想在 ASP.NET 网页中填充一个 gridview 控件,其中仅包含显示的行数所需的数据。 NHibernate 如何支持这一点?
【问题讨论】:
标签: .net nhibernate orm pagination
ICriteria 有一个SetFirstResult(int i) 方法,它指示您希望获取的第一个项目的索引(基本上是您页面中的第一个数据行)。
它还有一个SetMaxResults(int i) 方法,表示您希望获得的行数(即您的页面大小)。
例如,此条件对象获取数据网格的前 10 个结果:
criteria.SetFirstResult(0).SetMaxResults(10);
【讨论】:
您还可以利用 NHibernate 中的 Futures 功能来执行查询以获取总记录数以及单个查询中的实际结果。
示例
// 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>();
要获得总记录数,请执行以下操作:
int iRowCount = rowCount.Value;
here 是关于 Futures 给你的一个很好的讨论。
【讨论】:
从 NHibernate 3 及以上版本,您可以使用QueryOver<T>:
var pageRecords = nhSession.QueryOver<TEntity>()
.Skip((PageNumber - 1) * PageSize)
.Take(PageSize)
.List();
您可能还想像这样明确地对结果进行排序:
var pageRecords = nhSession.QueryOver<TEntity>()
.OrderBy(t => t.AnOrderFieldLikeDate).Desc
.Skip((PageNumber - 1) * PageSize)
.Take(PageSize)
.List();
【讨论】:
public IList<Customer> GetPagedData(int page, int pageSize, out long count)
{
try
{
var all = new List<Customer>();
ISession s = NHibernateHttpModule.CurrentSession;
IList results = s.CreateMultiCriteria()
.Add(s.CreateCriteria(typeof(Customer)).SetFirstResult(page * pageSize).SetMaxResults(pageSize))
.Add(s.CreateCriteria(typeof(Customer)).SetProjection(Projections.RowCountInt64()))
.List();
foreach (var o in (IList)results[0])
all.Add((Customer)o);
count = (long)((IList)results[1])[0];
return all;
}
catch (Exception ex) { throw new Exception("GetPagedData Customer da hata", ex); }
}
当分页数据是否有另一种方法可以从 MultiCriteria 获取键入的结果,或者每个人都像我一样做同样的事情?
谢谢
【讨论】:
如 Ayende 在 this blog post 中讨论的那样,使用 Linq to NHibernate 怎么样?
代码示例:
(from c in nwnd.Customers select c.CustomerID)
.Skip(10).Take(10).ToList();
这里是 NHibernate 团队博客Data Access With NHibernate 上的详细帖子,包括实现分页。
【讨论】:
您很可能希望在 GridView 中显示一个数据片段加上与您的查询匹配的数据总量的总行数(行数)。
您应该使用 MultiQuery 在一次调用中将 Select count(*) 查询和 .SetFirstResult(n).SetMaxResult(m) 查询发送到您的数据库。
请注意,结果将是一个包含 2 个列表的列表,一个用于数据切片,一个用于计数。
例子:
IMultiQuery multiQuery = s.CreateMultiQuery()
.Add(s.CreateQuery("from Item i where i.Id > ?")
.SetInt32(0, 50).SetFirstResult(10))
.Add(s.CreateQuery("select count(*) from Item i where i.Id > ?")
.SetInt32(0, 50));
IList results = multiQuery.List();
IList items = (IList)results[0];
long count = (long)((IList)results[1])[0];
【讨论】:
我建议您创建一个特定的结构来处理分页。类似的东西(我是一名 Java 程序员,但这应该很容易映射):
public class Page {
private List results;
private int pageSize;
private int page;
public Page(Query query, int page, int pageSize) {
this.page = page;
this.pageSize = pageSize;
results = query.setFirstResult(page * pageSize)
.setMaxResults(pageSize+1)
.list();
}
public List getNextPage()
public List getPreviousPage()
public int getPageCount()
public int getCurrentPage()
public void setPageSize()
}
我没有提供实现,但您可以使用@Jon 建议的方法。这是good discussion 供您查看。
【讨论】:
您不需要定义 2 个条件,您可以定义一个并克隆它。 要克隆 nHibernate 标准,您可以使用简单的代码:
var criteria = ... (your criteria initializations)...;
var countCrit = (ICriteria)criteria.Clone();
【讨论】: