代码之家  ›  专栏  ›  技术社区  ›  Russell Steen

分页IEnumerable数据集

  •  3
  • Russell Steen  · 技术社区  · 15 年前

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

    通过分页,我指的是指向当前记录的指针,以及实现以下概念的指针。

    .page size<-获取/设置页面大小 .last<-最后一页 .current<-当前页 .jumpto(页码)

    如果页面大小或设置大小发生变化,使用故障保险箱确保您最终到达正确的位置

    1 回复  |  直到 15 年前
        1
  •  2
  •   AxelEckenberger    15 年前

    你可以使用 PagedList 包装列表 by Rob Conery . 还有一个扩展版本 Troy Goode .

    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);
            }        
        }
    }