分页类

/// <summary>
    /// 分页列表类
    /// </summary>
    public class PagedList<T>
    {
        /// <summary>
        /// 每页页面大小
        /// </summary>
        public int PageSize { get; private set; }

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

        /// <summary>
        /// 首页页面Index
        /// </summary>
        public IEnumerable<T> Items { get; private set; }

        /// <summary>
        /// .ctor
        /// </summary>
        /// <param name="items">总列表</param>
        /// <param name="pageSize">每页大小</param>
        public PagedList(IEnumerable<T> items, int pageSize)
        {
            PageSize = pageSize;
            TotalCount = items.Count();
            Items = items;
        }

        /// <summary>
        /// 总页面数
        /// </summary>
        public int PageCount
        {
            get { return TotalCount % PageSize == 0 ? TotalCount / PageSize : TotalCount / PageSize + 1; }
        }

        /// <summary>
        /// 根据页面索引计算当前页在列表中的区间上限
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public int GetStartIndex(int pageIndex)
        {
            return (pageIndex - 1) * PageSize + 1;
        }

        /// <summary>
        /// 根据页面索引计算当前页在列表中的区间下限
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public int GetEndIndex(int pageIndex)
        {
            return (pageIndex * PageSize);
        }

        /// <summary>
        /// 根据页面索引获得列表中对应的数据
        /// </summary>
        /// <param name="pageIdx"></param>
        /// <returns></returns>
        public IEnumerable<T> SubItems(int pageIdx)
        {
            return this.Items.Skip(GetStartIndex(pageIdx)-1).Take(PageSize);
        }
    }

 

posted @ 2017-12-18 18:02  zslm___  阅读(142)  评论(0编辑  收藏  举报