/// <summary> /// 分页查询 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="hql">查询语句</param> /// <param name="skip">页数</param> /// <param name="take">每页显示条数</param> /// <param name="count">总数</param> /// <returns></returns> public static List <T> GetEntitiesByHQL <T>(string hql, int skip, int take, out int count) { BasicDataManipulation baseData = new BasicDataManipulation(); ISession Session = baseData.GetSession(); List <T> list = new List <T>(); if (Session == null) { throw new Exception("打开数据库连接失败!"); } try { NHibernate.IQuery query1 = Session.CreateQuery(hql); string hqlcount = ""; int fromIndex = hql.IndexOf("from"); int toIndex = hql.IndexOf("order"); int len = 0; if (toIndex == -1) { hqlcount = "select count(*) " + hql.Substring(fromIndex); } else { len = toIndex - fromIndex; hqlcount = "select count(*) " + hql.Substring(fromIndex, len); } NHibernate.IQuery querycount = Session.CreateQuery(hqlcount); try { count = int.Parse(querycount.UniqueResult().ToString()); } catch { count = 0; } NHibernate.IQuery query = query1.SetFirstResult(skip).SetMaxResults(take); list = query.List <T>().ToList(); return(list); } catch (Exception ex) { count = 0; LogAPI.Debug(ex); return(list); } finally { if (Session != null) { Session.Dispose(); Session = null; } } }
public IEnumerable <T> Get(NHibernate.IQuery query, int baseIndex, short pageSize, out int count, string sortFieldName, SortDirection sortDirection) { count = query.List <T>().Count; //if pageSize>0 then do paging, otherwise no paging and all records will be displayed if (pageSize > 0) { query.SetFirstResult(baseIndex) .SetMaxResults(pageSize) .SetMaxResults(pageSize); } IList <T> result = query.List <T>(); return(result); }
public IEnumerable <T> Get(NHibernate.IQuery query, int pageIndex, int pageSize, out int count, string sortFieldName, SortDirection sortDirection) { count = query.List <T>().Count; //if pageSize>0 then do paging, otherwise no paging and all records will be displayed if (pageSize > 0) { query.SetFirstResult((int)pageIndex * pageSize) .SetMaxResults(pageSize) .SetMaxResults(pageSize); } IList <T> result = query.List <T>(); //var rowCount = query.SetFirstResult(0).FutureValue<Int32>(); //count = rowCount.Value; return(result); }