新来同事,说hql 太麻烦,弄着头晕,正好3.2 linq 出来了。花了2天从新封装了下,以前根据codesmith 改的框架弄了下。
先把分页贴出来这个估计用的人多点:
分页的集成类
View Code
/// <summary> /// loswing add 2010 /// </summary> /// <typeparam name="T"></typeparam> public class WebPageOb<T> { /// <summary> /// 当前页面 /// </summary> private int _currentPage = 1; /// <summary> /// 排序标示 /// </summary> private WepPageOrder _pageOrder; /// <summary> /// 下一页是多少 /// </summary> private int _nextPage = 0; /// <summary> /// 上一页是多少 /// </summary> private int _lastpage = 0; /// <summary> /// 共计多少条 /// </summary> private int _amount = 0; /// <summary> /// 列表 /// </summary> private IList<T> _pageList; /// <summary> /// 每页显示多少条记录 /// </summary> private int _pageSize = 10; /// <summary> /// 现在list从第几条开 /// </summary> private int _pageBeginNum = 0; /// <summary> /// 一共多少页 /// </summary> private int _pageNums = 0; /// <summary> /// 列表 /// </summary> public IList<T> PageList { get { if(_pageList==null) _pageList = new List<T>(); return _pageList; } set { if(_pageList!=null) _pageList = null; _pageList = value; } } /// <summary> /// 当前页面 /// </summary> public int CurrentPage { get { return _currentPage; } set { _currentPage = value; } } /// <summary> /// 下一页是多少 /// </summary> public int NextPage { get { if (CurrentPage < PageNums) _nextPage = CurrentPage + 1; else { _nextPage = PageNums; } return _nextPage; } set { _nextPage = value; } } /// <summary> /// 上一页是多少 /// </summary> public int Lastpage { get { if (CurrentPage > 1) _lastpage = CurrentPage - 1; else { _lastpage = 1; } return _lastpage; } set { _lastpage = value; } } /// <summary> ///共计多少条 /// </summary> public int Amount { get { return _amount; } set { _amount = value; } } /// <summary> /// 每页显示多少条记录 /// </summary> public int PageSize { get { if (_pageSize <= 0) _pageSize = 1; return _pageSize; } set { _pageSize = value; } } /// <summary> /// 现在list从第几条开 /// </summary> public int PageBeginNum { get { //TODO check page return (CurrentPage - 1)*PageSize; } set { } } /// <summary> /// 一共多少页 /// </summary> public int PageNums { get { return (int) Math.Ceiling(((double)Amount/(double)PageSize)); } set { _pageNums = value; } } }
分页方法:
using NHibernate; using NHibernate.Criterion; using NUnit.Framework; using NetworkMonitor.DAL.Business.Model; using YNLD.DB.NHibernate; namespace NetworkMonitor.NunitTest.DAlTets { [TestFixture] public class 分页测试 { private readonly NetworkMonitor.DAL.Core.MyServiceFactory _service = NetworkMonitor.DAL.Core.MyServiceFactory.Instance; [Test] public void Ling分页() { var bb = _service.GetTbUserService.Dao(); QueryOver<TbUser> y1 = QueryOver.Of<TbUser>().OrderBy(o=>o.UserName).Asc; var back = this.GetPage<TbUser>(y1, bb.SessionFactory.GetSession(),9,10); // foreach (var user in back.PageList) { System.Console.WriteLine("userid begin = "+ back.PageList[0].UserName); System.Console.WriteLine("userid end = " + back.PageList[back.PageList.Count-1].UserName); System.Console.WriteLine("第{0}页,共{1}页,有{2}条数据",back.CurrentPage,back.PageNums,back.Amount); } } /// <summary> /// 分页方法 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="query"></param> /// <param name="session"></param> /// <param name="pageSize"></param> /// <param name="nowPage"></param> /// <returns></returns> public WebPageOb<T> GetPage<T>(QueryOver<T> query, ISession session,int pageSize,int nowPage) where T : class { WebPageOb<T> pageList = new WebPageOb<T>(); pageList.CurrentPage = pageSize; pageList.PageSize = nowPage; var cc = query.Clone(); var count = cc.ClearOrders().GetExecutableQueryOver(session).RowCount(); System.Console.WriteLine("count=={0}",count); pageList.Amount = count; query.Skip(pageList.PageBeginNum).Take(pageList.PageSize); pageList.PageList = query.GetExecutableQueryOver(session) .List(); return pageList; } } }