繁体   English   中英

如何使用 NHibernate 进行分页?

[英]How can you do paging with NHibernate?

例如,我想在 ASP.NET 网页中填充 gridview 控件,仅使用显示的行数所需的数据。 NHibernate 如何支持这一点?

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;

关于 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 的这篇博文中所讨论的那样,如何使用 Linq 到 NHibernate?

代码示例:

(from c in nwnd.Customers select c.CustomerID)
        .Skip(10).Take(10).ToList(); 

这是 NHibernate 团队博客关于使用 NHibernate进行数据访问的详细帖子,包括实现分页。

在 GridView 中,您很可能希望显示数据切片以及与您的查询匹配的数据总量的总行数 (rowcount)。

您应该使用 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建议的方法。 这是一个很好的讨论,供您查看。

您不需要定义 2 个条件,您可以定义一个并克隆它。 要克隆 nHibernate 标准,您可以使用简单的代码:

var criteria = ... (your criteria initializations)...;
var countCrit = (ICriteria)criteria.Clone();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM