tags:

views:

153

answers:

1

Are there any built in paging functions for IEnumberable (or a better library to use)? I know there is Take<>(), but I find myself repeatedly implementing the basic calculations to determine # of pages for a given page size. I realize it's simple implementation, but that's why I'm hoping it's already in the library and I just missed it.

By paging I mean a pointer to current record and something to fulfill the following concepts.

.PageSize <- get/set page size .Last <- last page .Current <- current page .JumpTo(pageNumber)

With the fail safes to make sure you end up in the right place if page size or set size change

+1  A: 

You can use the PagedList wrapper around a List by Rob Conery. There is also an extended version by 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);
        }        
    }
}
Obalix
Looks perfect, this is exactly what I was looking for
Russell Steen
On second blush, it's missing a few items, but it's a good place to start.
Russell Steen
Added another link to a slightly extended version. However, I think that you can implement you requirements from this base.
Obalix
Yep, it's a good start, thanks.
Russell Steen