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