C# 分页IEnumerable数据集

C# 分页IEnumerable数据集,c#,.net,C#,.net,IEnumberable(或更好的库)是否有内置的分页函数?我知道有Take(),但我发现自己反复执行基本计算,以确定给定页面大小的页面数。我意识到这是一个简单的实现,但这就是为什么我希望它已经在库中,而我只是错过了它 我所说的分页是指指向当前记录的指针和满足以下概念的东西 .PageSize您可以在列表周围使用PagedList包装器。还有一个扩展版本 使用系统; 使用System.Collections.Generic; 使用System.Linq; 命名空间System.Web.Mvc {

IEnumberable(或更好的库)是否有内置的分页函数?我知道有Take(),但我发现自己反复执行基本计算,以确定给定页面大小的页面数。我意识到这是一个简单的实现,但这就是为什么我希望它已经在库中,而我只是错过了它

我所说的分页是指指向当前记录的指针和满足以下概念的东西


.PageSize您可以在列表周围使用
PagedList
包装器。还有一个扩展版本

使用系统;
使用System.Collections.Generic;
使用System.Linq;
命名空间System.Web.Mvc
{
公共接口IPagedList
{
整数总数
{
得到;
设置
}
整型页面索引
{
得到;
设置
}
整型页面大小
{
得到;
设置
}
bool IsPreviousPage
{
得到;
}
布尔下一页
{
得到;
}     
}
公共类页面列表:列表,IPagedList
{
公共页面列表(IQueryable源、整型索引、整型页面大小)
{
this.TotalCount=source.Count();
this.PageSize=页面大小;
this.PageIndex=索引;
this.AddRange(source.Skip(index*pageSize).Take(pageSize.ToList());
}    
公共页面列表(列表源、整型索引、整型页面大小)
{
this.TotalCount=source.Count();
this.PageSize=页面大小;
this.PageIndex=索引;
this.AddRange(source.Skip(index*pageSize).Take(pageSize.ToList());
}
公共整数总数
{ 
获得;设置;
}
公共整数页索引
{ 
获得;设置;
}
公共int页面大小
{ 
获得;设置;
}
公共bool IsPreviousPage
{ 
得到
{
返回(页面索引>0);
}
}
公共图书馆下一页
{ 
得到
{

return(PageIndex*PageSize)看起来很完美,这正是我在《第二次脸红》中寻找的内容,它缺少一些项目,但这是一个很好的开始。添加了另一个链接到稍微扩展的版本。不过,我认为您可以从这个基础上实现您的需求。
using System;
using System.Collections.Generic;
using System.Linq;

namespace System.Web.Mvc
{
    public interface IPagedList
    {
        int TotalCount
        {
            get;
            set;
        }

        int PageIndex
        {
            get;
            set;
        }

        int PageSize
        {
            get;
            set;
        }

        bool IsPreviousPage
        {
            get;
        }

        bool IsNextPage
        {
            get;
        }     
    }

    public class PagedList<T> : List<T>, IPagedList
    {
        public PagedList(IQueryable<T> source, int index, int pageSize)
        {
            this.TotalCount = source.Count();
            this.PageSize = pageSize;
            this.PageIndex = index;
            this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
        }    

        public PagedList(List<T> source, int index, int pageSize)
        {
            this.TotalCount = source.Count();
            this.PageSize = pageSize;
            this.PageIndex = index;
            this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
        }

        public int TotalCount      
        { 
            get; set; 
        }

        public int PageIndex       
        { 
            get; set; 
        }

        public int PageSize 
        { 
            get; set; 
        }

        public bool IsPreviousPage 
        { 
            get 
            {
                return (PageIndex > 0);
            }
        }

        public bool IsNextPage 
        { 
            get
            {
                return (PageIndex * PageSize) <=TotalCount;
            } 
        }        
    }

    public static class Pagination
    {
        public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index, int pageSize) 
        {
            return new PagedList<T>(source, index, pageSize);
        }

        public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index)
        {
            return new PagedList<T>(source, index, 10);
        }        
    }
}