简体   繁体   English

Nhibernate QueryOver-获取分页的子计数

[英]Nhibernate QueryOver - Get Child Count for Pagination

I am using Nhibernate 3.0 and need to implement paging onto a site. 我正在使用Nhibernate 3.0,需要实现到站点的分页。 Basically we have a ProductCategory which has a collection of Products associated with it. 基本上,我们有一个ProductCategory,它具有与之关联的产品的集合。 So far I have this which does work 到目前为止,我有这个工作

var result = Session.QueryOver<TEntity>().TransformUsing(Transformers.DistinctRootEntity)
            .Where(category => category.CategoryId == criteria.CategoryId)
            .Fetch(category => category.Products).Eager
            .Take(pageSize)
            .Skip((pageIndex - 1)*pageSize)
            .Future<TEntity>();

This returns me the category I am requesting and the child products paged correctly based on the page size and page index passed in. 这将返回我所请求的类别,并根据传入的页面大小和页面索引正确地对子产品进行分页。

What I want to do now is actually get the total row count of the products, so for example, even though I am only returning 5 products for example, I need to know that there are 100 in total. 我现在要做的实际上是获得产品的总行数,因此,例如,即使我仅退回5个产品,我也要知道总共有100个产品。

Many Thanks 非常感谢

You should do that with another query by going from the Product side and employing .RowCount() . 您应该通过从Product端使用.RowCount()对另一个查询执行此操作。

Something like this (if your Product has a Category property): 这样的事情(如果您的Product具有Category属性):

int count = session.QueryOver<Product>()
    .Where(x => x.Category.Id == categoryId)
    .RowCount();

Here you can find several more ways to get count: How do I get row count using the NHibernate QueryOver api? 在这里,您可以找到其他几种获取计数的方法: 如何使用NHibernate QueryOver API获取行计数?

Another blog post that might be helpful to you: 另一篇可能对您有帮助的博客文章:
NHibernate 3.0 QueryOver - Searching, and Paging at database level NHibernate 3.0 QueryOver-在数据库级别搜索和分页

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

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