PageList`.cs 983 B

12345678910111213141516171819202122232425262728293031323334
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Bowin.Common
  6. {
  7. public class PageList<T>
  8. {
  9. /// <summary>
  10. /// 构造函数,当 pageIndex 或 pageSize 为null时,获取全部数据。
  11. /// </summary>
  12. /// <param name="query"></param>
  13. /// <param name="pageIndex">页码,从1开始</param>
  14. /// <param name="pageSize">页数</param>
  15. public PageList(IQueryable<T> query, int? pageIndex, int? pageSize)
  16. {
  17. if (pageSize.HasValue && pageIndex.HasValue)
  18. {
  19. Total = query.Count();
  20. Items = query.Skip((pageIndex.Value - 1) * pageSize.Value).Take(pageSize.Value);
  21. }
  22. else
  23. {
  24. Total = query.Count();
  25. Items = query;
  26. }
  27. }
  28. public int Total { get; private set; }
  29. public IQueryable<T> Items { get; private set; }
  30. }
  31. }