泛型分页

using System;
using System.Collections.Generic;
using System.Text;

namespace PublicClass
{
    public class PaginationInfo<T>
    {
        /// <summary>
        /// 每页记录数
        /// </summary>
        public int PageSize { get; private set; }
        /// <summary>
        /// 实际每页记录数,最后一页记录数可能少于每页记录数
        /// </summary>
        public int ActualPageSize { get; private set; }
        /// <summary>
        /// 页索引数
        /// </summary>
        public int PageIndex { get; private set; }
        /// <summary>
        /// 当页起始记录数,为sql server分页sql语句准备的数据
        /// </summary>
        public int PageStartCount { get; private set; }
        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalRecordCount { get; private set; }
        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPageCount { get; private set; }
        /// <summary>
        /// SQL server下,分页第一次查询的数量
        /// </summary>
        public long TopCount { get; private set; }
        /// <summary>
        /// 数据队列
        /// </summary>
        public List<T> PageContent { get; set; }
        /// <summary>
        /// 初始化分页
        /// </summary>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="pageIndex">当前页索引数</param>
        /// <param name="totalRecordCount">总记录数</param>
        protected PaginationInfo(int pageSize, int pageIndex, int totalRecordCount)
        {
            this.PageSize = pageSize;
            this.TotalRecordCount = totalRecordCount;
            this.PageIndex = pageIndex;
            PageContent = new List<T>();
            Compute();
        }
        /// <summary>
        /// 用于快速的生成一个分页数据
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="totalRecordCount"></param>
        /// <returns></returns>
        public static PaginationInfo<T> CreatePageData(int pageSize, int pageIndex, int totalRecordCount)
        {
            return new PaginationInfo<T>(pageSize, pageIndex, totalRecordCount);
        }
        /// <summary>
        /// 计算分页数据
        /// </summary>
        protected virtual void Compute()
        {
            #region -- 计算分页数据 --
            PageSize = PageSize < 1 ? 20 : PageSize;//默认20
            int lastPage = Convert.ToInt32(TotalRecordCount % PageSize);//计算最后一页记录数
            TotalPageCount = Convert.ToInt32(TotalRecordCount / PageSize) + (lastPage > 0 ? 1 : 0);//计算总页数
            PageIndex = PageIndex > TotalPageCount ? TotalPageCount : PageIndex;//检查当前页数大
            PageIndex = PageIndex < 1 ? 1 : PageIndex;//检查当前页小
            TopCount = PageIndex * PageSize;//sqlite中用的 top 多少记录数,比sql server少pagesize个
            ActualPageSize = (PageIndex == TotalPageCount && lastPage != 0) ? lastPage : PageSize;//判断是否最后一页,并指定页记录数
            PageStartCount = (PageIndex - 1) * PageSize;//sql server用的
            #endregion -- 计算分页完成 --
        }
        /// <summary>
        /// 添加一个数据
        /// </summary>
        /// <param name="item"></param>
        public void AddItem(T item)
        {
            if (this.PageContent != null)
            {
                this.PageContent.Add(item);
            }
        }
        /// <summary>
        /// 添加一组数据
        /// </summary>
        /// <param name="items"></param>
        public void AddItems(IEnumerable<T> items)
        {
            if (this.PageContent != null)
            {
                this.PageContent.AddRange(items);
            }
        }
    }
}

 将分页计算移到了sql之外进行,对于有存储过程的数据库来说有点多余! 可取舍!

posted on 2012-07-13 21:11  未来无限  阅读(761)  评论(0编辑  收藏  举报

导航