码迷,mamicode.com
首页 > 其他好文 > 详细

分页类

时间:2014-11-26 18:32:41      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:style   io   ar   color   sp   数据   on   bs   ad   

 

/// <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); }
        }
    }

分页类

标签:style   io   ar   color   sp   数据   on   bs   ad   

原文地址:http://www.cnblogs.com/jes_shaw/p/4123740.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!