Skip to content

X.PagedList

Andrew Gubskiy edited this page Jul 8, 2024 · 3 revisions

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.

Installation

Install X.PagedList from NuGet.

How use library

Features

  • IPagedList Interface: Defines the properties and methods required for a paged list.
  • PagedList Class: Implements IPagedList and 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.

Creating a Paged List

To create a paged list from an existing collection, use the PagedList class or the extension methods provided by PagedListExtensions.

Example

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);

Accessing Metadata

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.

Example

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}");

Extension Methods

The PagedListExtensions class provides convenient extension methods for creating paged lists directly from IEnumerable collections.

Example

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);

Interfaces and Classes

IPagedList Interface

public interface IPagedList<T> : IEnumerable<T>
{
    int PageCount { get; }
    int TotalItemCount { get; }
    int PageNumber { get; }
    int PageSize { get; }
    bool HasPreviousPage { get; }
    bool HasNextPage { get; }
}

PagedList Class

public class PagedList<T> : BasePagedList<T>, IPagedList<T>
{
    public PagedList(IEnumerable<T> superset, int pageNumber, int pageSize)
    {
        // Implementation here
    }
}

PagedListMetaData Class

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; }
}

PagedListExtensions Class

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);
    }
}

BasePagedList Class

public abstract class BasePagedList<T> : PagedListMetaData, IPagedList<T>
{
    // Implementation here
}

Clone this wiki locally