NHibernate(2) HQL 条件查询(QBC) - 分页,统计,排序

1、创建 ICriteria

   ICriteria iCriteria = session.CreateCriteria(typeof(IEPBooking));

  iCriteria.Add(Restrictions.Like("CityCode", criteria.CityId));

2、创建克隆对象

 var totalCriteria = iCriteria.Clone() as ICriteria;

 

3、统计总记录数

this.PageRecordCount =
Convert.ToInt32(totalCriteria.SetProjection(
Projections.Count((Projections.Property("Filed1")))).UniqueResult());

 

4、统计金额合计

this.PageProductRealMoney = Convert.ToDecimal(
totalCriteria.SetProjection(
Projections.Sum((Projections.Property("ProductRealMoney")))).UniqueResult());

 

5、排序

if (criteria.LastupdateAsc)
iCriteria.AddOrder(Order.Asc("Lastupdate"));
else
iCriteria.AddOrder(Order.Desc("Lastupdate"));

 

6、分页显示数据集

if (criteria.CurrentPageIndex > 0 && criteria.PageSize > 0)
  iCriteria.SetFirstResult((criteria.CurrentPageIndex - 1) * criteria.PageSize)
  .SetMaxResults(criteria.PageSize);

 

var data = iCriteria.List<IEPBooking>();

 

 

 

 

 

 

 

你可能感兴趣的:(Hibernate)