分页类_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 分页类

分页类

 2014/11/26 17:50:54  jes.shaw  程序员俱乐部  我要评论(0)
  • 摘要:///<summary>///分页///</summary>///<typeparamname="T">类型</typeparam>publicclassPagedList<T>:List<T>{///<summary>///缓存分页构造函数///</summary>///<paramname="source">数据源</param>///<
  • 标签:

 

class="code">/// <summary>
    ///     分页
    /// </summary>
    /// <typeparam name="T">类型</typeparam>
    public class PagedList<T> : List<T>
    {
        /// <summary>
        ///     缓存分页构造函数
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="pageIndex">当前页页码</param>
        /// <param name="pageSize">每页显示记录数</param>
        public PagedList(IQueryable<T> source, int pageIndex, int pageSize)
        {
            if (source == null || !source.Any())
                throw new ArgumentNullException("source");

            int total = source.Count();
            TotalCount = total;
            TotalPages = total/pageSize;

            if (total%pageSize > 0)
                TotalPages++;

            PageSize = pageSize;
            PageIndex = pageIndex;
            AddRange(source.Skip(pageIndex*pageSize).Take(pageSize).ToList());
        }

        /// <summary>
        ///     缓存分页构造函数
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="pageIndex">当前页页码</param>
        /// <param name="pageSize">每页显示记录数</param>
        public PagedList(IList<T> source, int pageIndex, int pageSize)
        {
            if (source == null || !source.Any())
                throw new ArgumentNullException("source");

            TotalCount = source.Count();
            TotalPages = TotalCount/pageSize;

            if (TotalCount%pageSize > 0)
                TotalPages++;

            PageSize = pageSize;
            PageIndex = pageIndex;
            AddRange(source.Skip(pageIndex*pageSize).Take(pageSize).ToList());
        }

        /// <summary>
        ///     非缓存分页构造函数
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="pageIndex">当前页页码</param>
        /// <param name="pageSize">每页显示记录数</param>
        /// <param name="totalCount">总记录数</param>
        public PagedList(IEnumerable<T> source, int pageIndex, int pageSize, int totalCount)
        {
            if (source == null || !source.Any())
                throw new ArgumentNullException("source");

            TotalCount = totalCount;
            TotalPages = TotalCount/pageSize;

            if (TotalCount%pageSize > 0)
                TotalPages++;

            PageSize = pageSize;
            PageIndex = pageIndex;
            AddRange(source);
        }

        /// <summary>
        ///     当前页页码
        /// </summary>
        public int PageIndex { get; private set; }

        /// <summary>
        ///     每页显示记录数
        /// </summary>
        public int PageSize { get; private set; }

        /// <summary>
        ///     总记录数
        /// </summary>
        public int TotalCount { get; private set; }

        /// <summary>
        ///     总页数
        /// </summary>
        public int TotalPages { get; private set; }

        /// <summary>
        ///     有上一页
        /// </summary>
        public bool HasPreviousPage
        {
            get { return (PageIndex > 0); }
        }

        /// <summary>
        ///     有下一页
        /// </summary>
        public bool HasNextPage
        {
            get { return (PageIndex + 1 < TotalPages); }
        }
    }
  • 相关文章
发表评论
用户名: 匿名