zoukankan      html  css  js  c++  java
  • 分享一个C#的分页类

    废话不说只有代码:

     1 using System.Linq;
     2 using System.Collections.Generic;
     3 
     4 namespace CommonLibrary
     5 {
     6     public class PagedList<T> : List<T>
     7     {
     8         #region Properties
     9 
    10         public int PageIndex { get; private set; }
    11 
    12         public int PageSize { get; private set; }
    13 
    14         public int TotalCount { get; private set; }
    15 
    16         public int TotalPages { get; private set; }
    17 
    18         public bool HasPreviousPage
    19         {
    20             get { return (PageIndex > 0); }
    21         }
    22         public bool HasNextPage
    23         {
    24             get { return (PageIndex + 1 < TotalPages); }
    25         }
    26 
    27         #endregion
    28        //http://www.cnblogs.com/roucheng/
    29         #region Constructors
    30 
    31         public PagedList(IQueryable<T> source, int pageIndex, int pageSize)
    32         {
    33             if (source == null || source.Count() < 1)
    34                 throw new System.ArgumentNullException("source");
    35 
    36             int total = source.Count();
    37             this.TotalCount = total;
    38             this.TotalPages = total / pageSize;
    39 
    40             if (total % pageSize > 0)
    41                 TotalPages++;
    42 
    43             this.PageSize = pageSize;
    44             this.PageIndex = pageIndex;
    45             this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
    46         }
    47 
    48         public PagedList(IList<T> source, int pageIndex, int pageSize)
    49         {
    50             if (source == null || source.Count() < 1)
    51                 throw new System.ArgumentNullException("source");
    52 
    53             TotalCount = source.Count();
    54             TotalPages = TotalCount / pageSize;
    55 
    56             if (TotalCount % pageSize > 0)
    57                 TotalPages++;
    58 
    59             this.PageSize = pageSize;
    60             this.PageIndex = pageIndex;
    61             this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
    62         }
    63 
    64         public PagedList(IEnumerable<T> source, int pageIndex, int pageSize, int totalCount)
    65         {
    66             if (source == null || source.Count() < 1)
    67                 throw new System.ArgumentNullException("source");
    68 
    69             TotalCount = totalCount;
    70             TotalPages = TotalCount / pageSize;
    71 
    72             if (TotalCount % pageSize > 0)
    73                 TotalPages++;
    74 
    75             this.PageSize = pageSize;
    76             this.PageIndex = pageIndex;
    77             this.AddRange(source);
    78         }
    79 
    80         #endregion
    81     }
    82 }
  • 相关阅读:
    ExtJS4学习笔记二--表单控件相关
    Js中replace()的用法
    浅析轮询(Polling)和推送(LongPolling)服务
    ExtJS4学习笔记五--面板使用
    ExtJS4学习笔记四--图片上传
    spring MVC
    ExtJS4学习笔记三--远程访问数据源示例
    Struts 2
    ExtJs4学习笔记一--基础知识
    URL编码规则
  • 原文地址:https://www.cnblogs.com/roucheng/p/3468498.html
Copyright © 2011-2022 走看看