第一次写博客 写的不好各位大神多多包涵。
我的分页主要是针对Linq 分页来写的,针对IEnumerable<T> 和 IQueryable<T> 类型数据分页。 开始上代码
创建接口:
public interface IPagedList { /// <summary> /// 总记录数 /// </summary> int TotalCount { get; set; } /// <summary> /// 总页数 /// </summary> int TotalPages { get; set; } /// <summary> /// 当前页 /// </summary> int PageIndex { get; set; } /// <summary> /// 页面大小 /// </summary> int PageSize { get; set; } /// <summary> /// 是否上一页 /// </summary> bool IsPreviousPage { get; } /// <summary> /// 是否下一页 /// </summary> bool IsNextPage { get; } }
泛型实体类:PagedQueryableToListModel<T>(实现IQueryable集合的数据),PagedCacheToListModel<T> (实现List集合的数据)我在这里重载构造函数实现根据参数不同调用不同构造函数 代码如下:
1 //PagedQueryableToListModel<T>泛型类 2 public class PagedQueryableToListModel<T> : List<T>,IPagedList 3 { 4 /// <summary> 5 /// 数据源为IQueryable的范型 6 /// </summary> 7 /// <param name="source">数据源</param> 8 /// <param name="index">当前页</param> 9 /// <param name="pageSize">页大小</param> 10 public PagedQueryableToListModel(IQueryable<T> source, int index, int pageSize) 11 { 12 //判断传过来的实体集是否为空 13 if(source != null) 14 { 15 int total = source.Count(); 16 this.TotalCount = total; 17 this.PageSize = pageSize; 18 this.TotalPages = total / pageSize; 19 if (total % pageSize > 0) 20 TotalPages++; 21 this.PageSize = PageSize; 22 if (index > this.TotalPages) 23 { 24 index = this.TotalPages; 25 } 26 if (index < 1) 27 { 28 index = 1; 29 } 30 this.PageIndex = index; 31 List<T> resultSet = source.Skip((PageIndex-1) * PageSize).Take(PageSize).ToList(); 32 this.AddRange(resultSet);//Skip跳过指定条数,Take返回多少条 33 } 34 } 35 36 /// <summary> 37 /// 数据源为IQueryable的范型 38 /// </summary> 39 /// <param name="source">数据源</param> 40 /// <param name="index">当前页</param> 41 /// <param name="pageSize">页大小</param> 42 public PagedQueryableToListModel(IQueryable<T> source, int index, int pageSize, out int totalCount) 43 { 44 //判断传过来的实体集是否为空 45 if (source != null) 46 { 47 totalCount = source.Count(); 48 this.TotalCount = totalCount; 49 this.PageSize = pageSize; 50 this.TotalPages = totalCount / pageSize; 51 if (totalCount % pageSize > 0) 52 TotalPages++; 53 this.PageSize = PageSize; 54 if (index > this.TotalPages) 55 { 56 index = this.TotalPages; 57 } 58 if (index < 1) 59 { 60 index = 1; 61 } 62 this.PageIndex = index; 63 List<T> resultSet = source.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList(); 64 this.AddRange(resultSet);//Skip跳过指定条数,Take返回多少条 65 } 66 else 67 { 68 totalCount = 0; 69 } 70 } 71 72 /// <summary> 73 /// 总记录数 74 /// </summary> 75 public int TotalCount { get; set; } 76 /// <summary> 77 /// 总页数 78 /// </summary> 79 public int TotalPages { get; set; } 80 /// <summary> 81 /// 当前页 82 /// </summary> 83 public int PageIndex { get; set; } 84 /// <summary> 85 /// 页面大小 86 /// </summary> 87 public int PageSize { get; set; } 88 /// <summary> 89 /// 是否上一页 90 /// </summary> 91 public bool IsPreviousPage 92 { 93 get 94 { 95 return PageIndex-1 > 0; 96 } 97 } 98 /// <summary> 99 /// 是否下一页 100 /// </summary> 101 public bool IsNextPage 102 { 103 get 104 { 105 return (PageIndex * PageSize) < TotalCount; 106 } 107 } 108 109 110 }
1 // PagedCacheToListModel<T>泛型类 2 public class PagedCacheToListModel<T> : List<T>, IPagedList 3 { 4 /// <summary> 5 /// 数据源为List的范型 6 /// </summary> 7 /// <param name="source">数据源</param> 8 /// <param name="index">当前页</param> 9 /// <param name="pageSize">页大小</param> 10 public PagedCacheToListModel(List<T> source, int index, int pageSize) 11 { 12 //判断传过来的实体集是否为空 13 if(source != null) 14 { 15 int total = source.Count; 16 this.TotalCount = total; 17 this.PageSize = pageSize; 18 this.TotalPages = total / pageSize; 19 if (total % pageSize > 0) 20 TotalPages++; 21 this.PageSize = PageSize; 22 if (index > this.TotalPages) 23 { 24 index = this.TotalPages; 25 } 26 if (index < 1) 27 { 28 index = 1; 29 } 30 this.PageIndex = index; 31 List<T> resultSet = source.Skip((PageIndex-1) * PageSize).Take(PageSize).ToList(); 32 this.AddRange(resultSet);//Skip跳过指定条数,Take返回多少条 33 } 34 } 35 36 /// <summary> 37 /// 数据源为List集合数据 38 /// </summary> 39 /// <param name="source">数据源</param> 40 /// <param name="index">当前页</param> 41 /// <param name="pageSize">页大小</param> 42 public PagedCacheToListModel(List<T> source, int index, int pageSize, out int totalCount) 43 { 44 //判断传过来的实体集是否为空 45 if (source != null) 46 { 47 totalCount = source.Count; 48 this.TotalCount = totalCount; 49 this.PageSize = pageSize; 50 this.TotalPages = totalCount / pageSize; 51 if (totalCount % pageSize > 0) 52 TotalPages++; 53 this.PageSize = PageSize; 54 if (index > this.TotalPages) 55 { 56 index = this.TotalPages; 57 } 58 if (index < 1) 59 { 60 index = 1; 61 } 62 this.PageIndex = index; 63 List<T> resultSet = source.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList(); 64 this.AddRange(resultSet);//Skip跳过指定条数,Take返回多少条 65 } 66 else 67 { 68 totalCount = 0; 69 } 70 71 } 72 73 /// <summary> 74 /// 总记录数 75 /// </summary> 76 public int TotalCount { get; set; } 77 /// <summary> 78 /// 总页数 79 /// </summary> 80 public int TotalPages { get; set; } 81 /// <summary> 82 /// 当前页 83 /// </summary> 84 public int PageIndex { get; set; } 85 /// <summary> 86 /// 页面大小 87 /// </summary> 88 public int PageSize { get; set; } 89 /// <summary> 90 /// 是否上一页 91 /// </summary> 92 public bool IsPreviousPage 93 { 94 get 95 { 96 return PageIndex-1 > 0; 97 } 98 } 99 /// <summary> 100 /// 是否下一页 101 /// </summary> 102 public bool IsNextPage 103 { 104 get 105 { 106 return (PageIndex * PageSize) < TotalCount; 107 } 108 } 109 110 }
构建泛型方法调用分页实体类
1 public static class ExtendPagedList 2 { 3 4 /// <summary> 5 /// static方法 传递linq返回分页数据 6 /// </summary> 7 /// <typeparam name="T">IQueryable集合</typeparam> 8 /// <param name="linq">数据源</param> 9 /// <param name="pageIndex">当前页</param> 10 /// <param name="pageSize">页面大小</param> 11 /// <returns></returns> 12 public static PagedQueryableToListModel<T> ToPagedList<T>(this IQueryable<T> linq, int pageIndex, int pageSize) 13 { 14 return new PagedQueryableToListModel<T>(linq, pageIndex, pageSize); 15 } 16 17 /// <summary> 18 /// static方法 传递linq返回分页数据 及 数据总数 19 /// </summary> 20 /// <typeparam name="T">IQueryable集合</typeparam> 21 /// <param name="linq"></param> 22 /// <param name="pageIndex"></param> 23 /// <param name="pageSize"></param> 24 /// <param name="totalCount">返回数据总数</param> 25 /// <returns></returns> 26 public static PagedQueryableToListModel<T> ToPagedList<T>(this IQueryable<T> linq, int pageIndex, int pageSize, out int totalCount) 27 { 28 return new PagedQueryableToListModel<T>(linq, pageIndex, pageSize, out totalCount); 29 } 30 /// <summary> 31 /// 返回List集合分页数据 32 /// </summary> 33 /// <typeparam name="T">List集合</typeparam> 34 /// <param name="list"></param> 35 /// <param name="pageIndex"></param> 36 /// <param name="pageSize"></param> 37 /// <returns></returns> 38 public static PagedCacheToListModel<T> PagedCacheToList<T>(this List<T> list, int pageIndex, int pageSize) 39 { 40 return new PagedCacheToListModel<T>(list, pageIndex, pageSize); 41 } 42 /// <summary> 43 /// 返回List集合分页数据 及 数据总数 44 /// </summary> 45 /// <typeparam name="T">List集合</typeparam> 46 /// <param name="list"></param> 47 /// <param name="pageIndex"></param> 48 /// <param name="pageSize"></param> 49 /// <param name="totalCount">返回总数</param> 50 /// <returns></returns> 51 public static PagedCacheToListModel<T> PagedCacheToList<T>(this List<T> list, int pageIndex, int pageSize, out int totalCount) 52 { 53 return new PagedCacheToListModel<T>(list, pageIndex, pageSize, out totalCount); 54 } 55 }
之后是Controller中ActionResult方法返回数据
1 public ActionResult Index(int indexPage=1, int pageSize=5) 2 { 3 XiaoMiViewModel miView = new XiaoMiViewModel(); 4 using(XiaoMiEntities db =new XiaoMiEntities()) 5 { 6 //IQueryable集合 7 var xiaomiSet1 = (from i in db.xiaomi 8 orderby i.id 9 select new XiaoMiModel 10 { 11 id = i.id, 12 username = i.username, 13 email = i.email 14 }).AsQueryable(); 15 16 int totalCount = default(int);//总数 17 //此处为IQueryable集合 18 miView.PageData = ExtendPagedList.ToPagedList(xiaomiSet1, indexPage, pageSize, out totalCount); 19 20 //list集合数据 21 var xiaomiSet2 = (from i in db.xiaomi 22 orderby i.id 23 select new XiaoMiModel 24 { 25 id = i.id, 26 username = i.username, 27 email = i.email 28 }).ToList(); 29 //此处为list数据集合 30 miView.CachePageData = ExtendPagedList.PagedCacheToList(xiaomiSet2, indexPage, pageSize, out totalCount); 31 miView.TotalCount = totalCount; 32 } 33 return View(miView); 34 }
在razor视图中展示分页数据
@model PagedListModel.ViewModels.XiaoMiViewModel @{ ViewBag.Title = "Index"; } <h2>IQueryable集合(sql直接分页)数据分页</h2> @foreach (var Data in Model.PageData.ToList()) { <p>ID:@Data.id : @Data.username</p> } <!--IQueryable集合(sql直接分页)数据分页 Start--> <p> 总数:@Model.TotalCount <br /> @if (Model.PageData.IsPreviousPage) { <a href="@Url.Action("Index", "Test", new { indexPage = Model.PageData.PageIndex - 1 })">上一页</a> } else { <em style="color:Gray">上一页</em> } @if (Model.PageData.IsNextPage) { <a href="@Url.Action("Index", "Test", new { indexPage = Model.PageData.PageIndex + 1 })">下一页</a> } else { <em style="color:Gray">下一页</em> } </p> <br /> <hr /> <h2>List数据直接分页</h2> <!--end--> @foreach (var Data in Model.CachePageData.ToList()) { <p>ID:@Data.id : @Data.username</p> } <!--直接list集合数据分页 Start--> <p> 总数:@Model.TotalCount <br /> @if (Model.CachePageData.IsPreviousPage) { <a href="@Url.Action("Index", "Test", new { indexPage = Model.CachePageData.PageIndex - 1 })">上一页</a> } else { <em style="color:Gray">上一页</em> } @if (Model.CachePageData.IsNextPage) { <a href="@Url.Action("Index", "Test", new { indexPage = Model.CachePageData.PageIndex + 1 })">下一页</a> } else { <em style="color:Gray">下一页</em> } </p> <!--end-->
好了 以上就是分页所有步骤了 。。。。
Mvc Linq 分页 参考PagedList
时间: 2024-10-29 19:07:08