using System; using System.Collections.Generic; using System.Linq; namespace System.Web.Mvc { public interface IPagedList { int ItemCount { get; set; } int PageCount { get; set; } int PageIndex { get; set; } int PageSize { get; set; } bool IsPreviousPage { get; } bool IsNextPage { get; } } public interface IPagedList : IList, IPagedList { } public class PagedList : List, IPagedList { public PagedList(IQueryable source, int index, int pageSize) { this.ItemCount = source.Count(); this.PageSize = pageSize; this.PageIndex = index; this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList()); this.PageCount = (int)Math.Ceiling((double)this.ItemCount / this.PageSize); } public PagedList(List source, int index, int pageSize) { this.ItemCount = source.Count(); this.PageSize = pageSize; this.PageIndex = index; this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList()); } public int ItemCount { get; set; } public int PageCount { 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+1) * PageSize <= ItemCount; } } } public static class Pagination { public static PagedList ToPagedList(this IQueryable source, int index, int pageSize) { return new PagedList(source, index, pageSize); } public static PagedList ToPagedList(this IQueryable source, int index) { return new PagedList(source, index, 10); } } }