zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    .net core2.2
    9_山寨系统调用 SystemCallEntry
    7_API调用
    8_InlineHook
    6_再次开中断STI的正确姿势
    5_中断现场下
    4_中断现场中
    3_中断现场上
    2_多核复杂性
    1_中断提权
  • 原文地址:https://www.cnblogs.com/wsion/p/4265471.html
Copyright © 2011-2022 走看看