Repository navigation
X.PagedList
X.PagedList is a library for paging through collections of data in .NET applications. This library provides easy-to-use and flexible methods for creating paged lists from existing collections. It can be used with any .NET application where pagination is needed, providing features to support server-side and client-side paging.
Install X.PagedList from NuGet.
- IPagedList Interface: Defines the properties and methods required for a paged list.
-
PagedList Class: Implements
IPagedListand provides functionalities to split collections into pages. - PagedListMetaData Class: Contains metadata information about a paged list.
- PagedListExtensions Class: Provides extension methods for creating paged lists.
- BasePagedList Class: An abstract base class that provides common functionalities for paged lists.
To create a paged list from an existing collection, use the PagedList class or the extension methods provided by PagedListExtensions.
using X.PagedList;
// Assuming you have a collection of items
IEnumerable<MyItem> items = GetItems();
// Create a paged list with a specified page number and page size
int pageNumber = 1;
int pageSize = 10;
IPagedList<MyItem> pagedList = new PagedList<MyItem>(items, pageNumber, pageSize);The PagedListMetaData class provides useful metadata about the paged list, such as total item count, total page count, and whether there are previous or next pages.
PagedListMetaData metaData = pagedList.GetMetaData();
Console.WriteLine($"Total Items: {metaData.TotalItemCount}");
Console.WriteLine($"Total Pages: {metaData.PageCount}");
Console.WriteLine($"Has Previous Page: {metaData.HasPreviousPage}");
Console.WriteLine($"Has Next Page: {metaData.HasNextPage}");The PagedListExtensions class provides convenient extension methods for creating paged lists directly from IEnumerable collections.
using X.PagedList.Extensions;
// Assuming you have a collection of items
IEnumerable<MyItem> items = GetItems();
// Create a paged list using extension method
int pageNumber = 1;
int pageSize = 10;
IPagedList<MyItem> pagedList = items.ToPagedList(pageNumber, pageSize);public interface IPagedList<T> : IEnumerable<T>
{
int PageCount { get; }
int TotalItemCount { get; }
int PageNumber { get; }
int PageSize { get; }
bool HasPreviousPage { get; }
bool HasNextPage { get; }
}public class PagedList<T> : BasePagedList<T>, IPagedList<T>
{
public PagedList(IEnumerable<T> superset, int pageNumber, int pageSize)
{
// Implementation here
}
}public class PagedListMetaData
{
public int PageCount { get; }
public int TotalItemCount { get; }
public int PageNumber { get; }
public int PageSize { get; }
public bool HasPreviousPage { get; }
public bool HasNextPage { get; }
}public static class PagedListExtensions
{
public static IPagedList<T> ToPagedList<T>(this IEnumerable<T> superset, int pageNumber, int pageSize)
{
return new PagedList<T>(superset, pageNumber, pageSize);
}
}public abstract class BasePagedList<T> : PagedListMetaData, IPagedList<T>
{
// Implementation here
}