IQueryable定义一个扩展方法。分页

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace page
{
    public class PagedList<T> : List<T>
    {
        public PagedList(IEnumerable<T> content, int currentPage, int pageSize, int totalCount)
            : this(totalCount, currentPage, pageSize)
        {
            AddRange(content);
        }
        public PagedList(IQueryable<T> source, int currentPage, int pageSize)
            : this(source.Count(), currentPage, pageSize)
        {
            AddRange(source.Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList());
        }

        public PagedList(IEnumerable<T> source, int currentPage, int pageSize)
            : this(source.Count(), currentPage, pageSize)
        {
            AddRange(source.Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList());
        }

        protected PagedList(int count, int currentPage, int pageSize)
        {
            TotalCount = count;
            PageSize = Math.Max(pageSize, 1);
            CurrentPage = Math.Min(Math.Max(currentPage, 1), TotalPages);
        }

        public int CurrentPage { get; set; }

        public bool HasPreviousPage
        {
            get { return CurrentPage > 1; }
        }

        public bool HasNextPage
        {
            get { return CurrentPage < TotalPages; }
        }

        public int PageSize { get; set; }

        public int TotalCount { get; set; }

        public int TotalPages
        {
            get { return Math.Max((TotalCount + PageSize - 1) / PageSize, 1); }
        }
    }
}

 

转载于:https://www.cnblogs.com/wlwjc/archive/2012/05/16/2505427.html

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页