1. 程式人生 > >分頁類

分頁類

style ets sum pan pos tor name 獲得 items

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

分頁類