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