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);
  • 相关阅读:
    Spring基于注解整合Redis实现内容缓存
    配置Mybatis二级缓存为Redis来实现内容缓存
    Spring整合Redis
    Java连接redis
    机器学习之 KNN近邻算法(一)入门
    matplotlib 之 快速入门
    Pandas 之入门
    Hadoop 之 环形缓冲区原理
    numpy 之 rollaxis的理解
    python 之 遇到SyntaxError: Non-UTF-8 code starting with 'xb8' in file
  • 原文地址:https://www.cnblogs.com/wsion/p/4265471.html
Copyright © 2011-2022 走看看