EFPagination 1.1.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package EFPagination --version 1.1.2
                    
NuGet\Install-Package EFPagination -Version 1.1.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EFPagination" Version="1.1.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EFPagination" Version="1.1.2" />
                    
Directory.Packages.props
<PackageReference Include="EFPagination" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EFPagination --version 1.1.2
                    
#r "nuget: EFPagination, 1.1.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package EFPagination@1.1.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EFPagination&version=1.1.2
                    
Install as a Cake Addin
#tool nuget:?package=EFPagination&version=1.1.2
                    
Install as a Cake Tool

Entity Framework Pagination (EFPagination)

Keyset pagination for Entity Framework Core. Also known as seek or cursor pagination.

Keyset pagination delivers stable query performance regardless of page depth, unlike offset pagination (Skip/Take) which degrades linearly as you skip more rows.

Installation

dotnet add package EFPagination

Requires .NET 10 and EF Core 10.

Quick Start

using EFPagination;

// Quick-start local variable; in production, build once and store as a static readonly field.
var definition = PaginationQuery.Build<User>(b => b.Descending(x => x.Created).Ascending(x => x.Id));

// First page.
var context = dbContext.Users.Paginate(definition);
var users = await context.Query.Take(20).ToListAsync();
context.EnsureCorrectOrder(users);

// Next page — pass the last item as the reference.
var nextContext = dbContext.Users.Paginate(definition, PaginationDirection.Forward, users[^1]);
var nextUsers = await nextContext.Query.Take(20).ToListAsync();
nextContext.EnsureCorrectOrder(nextUsers);

// Check boundaries.
var hasPrevious = await nextContext.HasPreviousAsync(nextUsers);
var hasNext = await nextContext.HasNextAsync(nextUsers);

What's Included

  • Prebuilt and inline keyset pagination over IQueryable<T>
  • Strongly-typed reference overloads to avoid loose-typing overhead
  • Runtime sort definitions via PaginationQuery.Build<T>(string, ...)
  • Opaque cursor token encoding/decoding with PaginationCursor
  • Direct ColumnValue pagination APIs for member-access definitions
  • Page execution with optional total-count retrieval via PaginationExecutor, including direct ColumnValue input
  • Sort field registries for request-driven sorting via PaginationSortRegistry<T>
  • Roslyn analyzer support for nullable pagination columns

API

Paginate

Applies keyset-based ordering and filtering to a query.

// With a prebuilt definition (recommended — enables expression caching):
PaginationContext<T> Paginate<T>(
    this IQueryable<T> source,
    PaginationQueryDefinition<T> queryDefinition,
    PaginationDirection direction = PaginationDirection.Forward,
    object? reference = null);

// With an inline builder:
PaginationContext<T> Paginate<T>(
    this IQueryable<T> source,
    Action<PaginationBuilder<T>> builderAction,
    PaginationDirection direction = PaginationDirection.Forward,
    object? reference = null)

// With a strongly-typed reference:
PaginationContext<T> Paginate<T, TReference>(
    this IQueryable<T> source,
    PaginationQueryDefinition<T> queryDefinition,
    PaginationDirection direction,
    TReference reference)

// With definition-bound ordered values:
PaginationContext<T> Paginate<T>(
    this IQueryable<T> source,
    PaginationQueryDefinition<T> queryDefinition,
    PaginationDirection direction,
    PaginationValues<T> referenceValues)

// With direct column values:
PaginationContext<T> Paginate<T>(
    this IQueryable<T> source,
    PaginationQueryDefinition<T> queryDefinition,
    PaginationDirection direction,
    ReadOnlySpan<ColumnValue> referenceValues)

PaginateQuery

Shortcut that returns the IQueryable<T> directly when you don't need the context:

// With a prebuilt definition (recommended):
IQueryable<T> PaginateQuery<T>(
    this IQueryable<T> source,
    PaginationQueryDefinition<T> queryDefinition,
    PaginationDirection direction = PaginationDirection.Forward,
    object? reference = null);

// With an inline builder:
IQueryable<T> PaginateQuery<T>(
    this IQueryable<T> source,
    Action<PaginationBuilder<T>> builderAction,
    PaginationDirection direction = PaginationDirection.Forward,
    object? reference = null)

// With a strongly-typed reference:
IQueryable<T> PaginateQuery<T, TReference>(
    this IQueryable<T> source,
    PaginationQueryDefinition<T> queryDefinition,
    PaginationDirection direction,
    TReference reference)

// With definition-bound ordered values:
IQueryable<T> PaginateQuery<T>(
    this IQueryable<T> source,
    PaginationQueryDefinition<T> queryDefinition,
    PaginationDirection direction,
    PaginationValues<T> referenceValues)

// With direct column values:
IQueryable<T> PaginateQuery<T>(
    this IQueryable<T> source,
    PaginationQueryDefinition<T> queryDefinition,
    PaginationDirection direction,
    ReadOnlySpan<ColumnValue> referenceValues)
var query = dbContext.Users
    .PaginateQuery(definition, direction, reference)
    .Take(20);

PaginationContext<T>

Returned by Paginate.

Property Description
Query The filtered and ordered IQueryable<T>. Chain .Take(), .Select(), etc.
OrderedQuery The ordered IQueryable<T> without the pagination filter. Used internally by boundary checks.
Direction The PaginationDirection used for this call.

Extension methods on PaginationContext<T>:

Method Description
EnsureCorrectOrder<T2>(IList<T2>) Reverses the list in-place when direction is Backward. Always call after fetching.
HasPreviousAsync<T2>(IReadOnlyList<T2>) Returns Task<bool> — true if there is data before the current page.
HasNextAsync<T2>(IReadOnlyList<T2>) Returns Task<bool> — true if there is data after the current page.

PaginationQueryDefinition<T>

A prebuilt, reusable pagination definition. Build once, reuse across requests:

static readonly PaginationQueryDefinition<User> Definition =
    PaginationQuery.Build<User>(b => b.Descending(x => x.Created).Ascending(x => x.Id));

PaginationQuery

Factory for building reusable definitions:

PaginationQueryDefinition<T> Build<T>(Action<PaginationBuilder<T>> builderAction)

PaginationQueryDefinition<T> Build<T>(
    string propertyName,
    bool descending,
    string? tiebreaker = "Id",
    bool tiebreakerDescending = false)

Use the string overload when sort fields come from request parameters or other runtime configuration.

PaginationBuilder<T>

Fluent builder for defining pagination columns, used inside PaginationQuery.Build<T>():

Method Description
Ascending<TCol>(Expression<Func<T, TCol>>) Adds a column with ascending sort.
Descending<TCol>(Expression<Func<T, TCol>>) Adds a column with descending sort.
ConfigureColumn<TCol>(Expression<Func<T, TCol>>, bool isDescending) Adds a column with explicit sort direction. Useful when the direction is dynamic.

Nested properties are supported: b.Ascending(x => x.Details.Created).

PaginationExecutor / KeysetPage<T>

Execute a page query and optionally fetch the total count:

Task<KeysetPage<T>> ExecuteAsync<T>(
    IQueryable<T> query,
    PaginationQueryDefinition<T> definition,
    int pageSize,
    object? reference,
    bool includeCount,
    CancellationToken ct = default)
    where T : class

Task<KeysetPage<T>> ExecuteAsync<T>(
    IQueryable<T> query,
    PaginationQueryDefinition<T> definition,
    int pageSize,
    bool includeCount,
    PaginationValues<T> referenceValues,
    CancellationToken ct = default)
    where T : class

Task<KeysetPage<T>> ExecuteAsync<T>(
    IQueryable<T> query,
    PaginationQueryDefinition<T> definition,
    int pageSize,
    bool includeCount,
    ReadOnlySpan<ColumnValue> referenceValues,
    CancellationToken ct = default)
    where T : class

Use the PaginationValues<T> overload as the canonical high-performance path for definition-based cursor decoding. Use the ColumnValue overload only when you already have manual member-addressable name/value pairs.

KeysetPage<T> contains:

Property Description
Items The page items.
HasMore true when another page exists after the current page.
TotalCount Total rows when includeCount is true; otherwise -1.

PaginationCursor / ColumnValue / PaginationCursorOptions

Encode and decode opaque cursor tokens that preserve typed keyset values and optional metadata:

string Encode(ReadOnlySpan<ColumnValue> values, PaginationCursorOptions? options = null)
bool TryDecode(ReadOnlySpan<char> encoded, Span<ColumnValue> values, out int written)
bool TryDecode(
    ReadOnlySpan<char> encoded,
    Span<ColumnValue> values,
    out int written,
    out string? sortBy,
    out int? totalCount)
bool TryDecode<T>(
    ReadOnlySpan<char> encoded,
    PaginationQueryDefinition<T> definition,
    out PaginationValues<T> values,
    out int written)
bool TryDecode<T>(
    ReadOnlySpan<char> encoded,
    PaginationQueryDefinition<T> definition,
    out PaginationValues<T> values,
    out int written,
    out string? sortBy,
    out int? totalCount)

Supported cursor value types include strings, booleans, numeric primitives, Guid, DateTime, DateTimeOffset, DateOnly, TimeOnly, TimeSpan, and enums.

ColumnValue-based pagination works for member-access definitions such as x => x.Created or x => x.Id. Computed expressions such as x => x.CreatedNullable ?? DateTime.MinValue remain supported through regular reference-object overloads, but they are not addressable by ColumnValue.Name.

Use the definition-based TryDecode overload when you want a ready-to-use PaginationValues<T> container for PaginationExecutor, Paginate, or PaginateQuery without manually pre-populating column names.

Example:

var page = await PaginationExecutor.ExecuteAsync(dbContext.Users, definition, 20, reference: null, includeCount: true);

var last = page.Items[^1];
var nextCursor = PaginationCursor.Encode(
[
    new ColumnValue(nameof(User.Created), last.Created),
    new ColumnValue(nameof(User.Id), last.Id),
],
new PaginationCursorOptions(TotalCount: page.TotalCount));

if (PaginationCursor.TryDecode(nextCursor, definition, out var values, out _))
{
    var nextPage = await PaginationExecutor.ExecuteAsync(
        dbContext.Users,
        definition,
        20,
        includeCount: false,
        values);
}

PaginationSortRegistry<T> / SortField<T>

Map request sort names to prebuilt definitions:

var sorts = new PaginationSortRegistry<User>(
    defaultDefinition: PaginationQuery.Build<User>(b => b.Descending(x => x.Created).Ascending(x => x.Id)),
    new SortField<User>(
        "created",
        PaginationQuery.Build<User>("Created", descending: false, tiebreaker: "Id"),
        PaginationQuery.Build<User>("Created", descending: true, tiebreaker: "Id")));

var definition = sorts.Resolve(sortBy, sortDir);

Resolve is case-insensitive and falls back to the default definition when the requested sort field is empty or unknown.

IncompatibleReferenceException

Thrown when a reference object is missing a property required by the pagination definition (loose typing mismatch).

Property Description
PropertyName The property that was not found on the reference object.
ReferenceType The type of the reference object that was searched.
EntityType The entity type that defines the pagination column.

PaginationDirection

enum PaginationDirection { Forward, Backward }
  • Forward — walk in the configured sort order (toward next pages).
  • Backward — walk against the configured sort order (toward previous pages).

Documentation

Topic Description
Getting Started Installation, requirements, first query
Pagination Patterns First, last, previous, next page patterns
API Reference Full API details, including cursor, executor, and sort-registry APIs
Prebuilt Definitions Caching pagination definitions for performance
Database Indexing Composite indexes, deterministic definitions
NULL Handling Computed columns, expression coalescing
Loose Typing DTOs, projections, anonymous type references
Analyzers & Diagnostics Build-time warnings and fixes

Samples

The samples directory contains a Razor Pages demo with four pagination variations and a Minimal API endpoint showcasing loose typing with opaque cursor tokens.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on EFPagination:

Package Downloads
EFPagination.AspNetCore

ASP.NET Core integration for EFPagination — model binders, response types, and minimal API helpers for cursor-based keyset pagination.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.2.0 299 8/21/2026
3.1.0 140 8/21/2026
3.0.3 727 6/18/2026
3.0.2 158 5/29/2026
3.0.1 139 5/26/2026
3.0.0 150 5/14/2026
2.0.0 172 3/28/2026
1.1.3 146 3/27/2026
1.1.2 138 3/27/2026
1.1.1 131 3/27/2026
1.1.0 135 3/27/2026
1.0.0 149 2/22/2026