首页 > 代码库 > 自定义分页

自定义分页

using System;
using System.Collections.Generic;
using System.Linq;
using Water;
namespace Water.Helper
{
    public class PagedList<T> : List<T>
    {

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

        public PagedList(List<T> source, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

            this.AddRange(source.Skip((PageIndex-1) * PageSize).Take(PageSize));
        }

        public PagedList(IQueryable<T> source, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

            this.AddRange(source.Skip((PageIndex-1) * PageSize).Take(PageSize));
        }

        public PagedList()
        {
            // TODO: Complete member initialization
        }
        /// <summary>
        /// 是否包含上一页
        /// </summary>
        public bool HasPreviousPage
        {
            get
            {
                return (PageIndex > 1);
            }
        }

        public bool HasNextPage
        {
            get
            {
                return (PageIndex  < TotalPages);
            }
        }

        internal void InsertRange(int p, Models.Admins prolistItem)
        {
            throw new NotImplementedException();
        }
    }
}

 

自定义分页