首页 > 代码库 > LINQ分页工具
LINQ分页工具
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);
LINQ分页工具
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。