PageLinqExtensions.cs

    1 /*
    2  ASP.NET MvcPager control
    3  Copyright:2009-2010 Webdiyer (http://en.webdiyer.com)
    4  Source code released under Ms-PL license
    5  */
    1 using System.Linq;
    2 
    3 namespace Webdiyer.WebControls.Mvc
    4 {
    5     public static class PageLinqExtensions
    6     {
    7         public static PagedList<T> ToPagedList<T>
    8             (
    9                 this IQueryable<T> allItems,
   10                 int pageIndex,
   11                 int pageSize
   12             )
   13         {
   14             if (pageIndex < 1)
   15                 pageIndex = 1;
   16             var itemIndex = (pageIndex-1) * pageSize;
   17             var pageOfItems = allItems.Skip(itemIndex).Take(pageSize);
   18             var totalItemCount = allItems.Count();
   19             return new PagedList<T>(pageOfItems, pageIndex, pageSize, totalItemCount);
   20         }
   21     }
   22 }