using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Collections; /// <summary> /// Page helper, default page size = 10 /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="entity"></typeparam> public class LinqPageHelper<T, entity> where T : IEnumerable<entity> { T _collection; int _pageCount; int _pageSize; public static readonly int defaultPageSize = 10; /// <summary> /// Capacity of a single page /// </summary> public int PageSize { get { return _pageSize; } set { _pageSize = value; onPageSizeChanged(); } } /// <summary> /// Total page count /// </summary> public int PageCount { get { return _pageCount; } set { _pageCount = value; } } /// <summary> /// Total count /// </summary> public int Count { get { return _collection.Count(); } } private LinqPageHelper() { } public LinqPageHelper(T t) : this(t, defaultPageSize) { } public LinqPageHelper(T t, int pageSize) { _collection = t; _pageSize = pageSize; onPageSizeChanged(); } private void onPageSizeChanged() { _pageCount = (_collection.Count() - 1 + _pageSize) / _pageSize; } public IEnumerable<entity> Take(int currentPage) { if (currentPage <= 0 || currentPage > PageCount) { return null; } var query = _collection.Take(_pageSize * currentPage).Skip(_pageSize * (currentPage - 1)); return query; } public bool IsLastPage(int currentPage) { return currentPage == _pageCount; } }
eg.
LinqPageHelper<IEnumerable<DataEntity>, DataEntity> helper = new LinqPageHelper<IEnumerable<DataEntity>, DataEntity>(list); helper.PageSize = pageSize; var result = helper.Take(currentPage);
时间: 2024-10-10 03:34:16