I have been trying to implement paging for quite a while now and I found this tutorial for paging with MVC: ASP.NET MVC Paging Done Perfectly
Now, in this solution,
The example on github illustrates that it is using an IQueryable which is then utilised by ToPagedList(), which implies that the code is pretty optimised and will not in itself return all records...
Looking at the code of class PagedList
// superset is the IQueryable.
TotalItemCount = superset == null ? 0 : superset.Count();
// add items to internal list
if (superset != null && TotalItemCount > 0)
Subset.AddRange(pageNumber == 1
? superset.Skip(0).Take(pageSize).ToList()
: superset.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList()
So as you can see it already uses the recommended server side paging methods of skip and take and then preforms a ToList().
However if you were not working with an IQueryable, i.e. an IEnumerable then the following code is used:
///
/// Initializes a new instance of the class that divides the supplied superset into subsets the size of the supplied pageSize. The instance then only containes the objects contained in the subset specified by index.
///
/// The collection of objects to be divided into subsets. If the collection implements , it will be treated as such.
/// The one-based index of the subset of objects to be contained by this instance.
/// The maximum size of any individual subset.
/// The specified index cannot be less than zero.
/// The specified page size cannot be less than one.
public PagedList(IEnumerable superset, int pageNumber, int pageSize)
: this(superset.AsQueryable(), pageNumber, pageSize)
{
}
The issue is that depending upon the filtering utilised to get the IEnumerable in the first place could contain all records, so use an IQueryable where possible for optimal performance of PagedList.