Dacke.DapperPaging.Offset 1.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package Dacke.DapperPaging.Offset --version 1.0.4
                    
NuGet\Install-Package Dacke.DapperPaging.Offset -Version 1.0.4
                    
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="Dacke.DapperPaging.Offset" Version="1.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Dacke.DapperPaging.Offset" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="Dacke.DapperPaging.Offset" />
                    
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 Dacke.DapperPaging.Offset --version 1.0.4
                    
#r "nuget: Dacke.DapperPaging.Offset, 1.0.4"
                    
#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 Dacke.DapperPaging.Offset@1.0.4
                    
#: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=Dacke.DapperPaging.Offset&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=Dacke.DapperPaging.Offset&version=1.0.4
                    
Install as a Cake Tool

DapperPaging.Offset

A high-performance, lightweight offset paging extension library for Dapper and SQL Server. Built on top of Paging.Common, DapperPaging.Offset parses complex filtering and sorting structures directly into parameterized SQL fragments and Dapper DynamicParameters to eliminate LINQ-to-SQL performance overhead.

Features

  • Parameterized SQL Generation: Safe SQL fragment generation avoiding SQL injection vulnerabilities.
  • Dynamic WHERE Builders: Converts nested FilterRequest collections into structured WHERE clauses with full AND/OR support.
  • Column Mapping Support: Maps incoming DTO property names to database columns dynamically using propertyMap dictionaries.
  • Single Round-Trip Execution: Designed to work seamlessly with Dapper's QueryMultipleAsync to fetch record counts and page items in a single database round-trip.
  • Strict SQL Server Compatibility: Generates standard OFFSET @Skip ROWS FETCH NEXT @Take ROWS ONLY syntax.

Installation

Install via the .NET CLI:

dotnet add package DapperPaging.Offset

Or via the Package Manager Console:

Install-Package DapperPaging.Offset

Key Extension Methods

1. BuildSqlWhere

Generates a parameterized SQL WHERE clause string and populates a Dapper DynamicParameters collection.

public static (string WhereClause, DynamicParameters Parameters) BuildSqlWhere(this PagedRequest request)

2. BuildSqlSorting

Generates a SQL ORDER BY clause string, falling back to a default sort when SortBy is not specified.

public static string BuildSqlSorting(
    this PagedRequest request, 
    IDictionary<string, string>? propertyMap = null, 
    string defaultSortColumn = "Id")

Usage Example

Below is an example of integrating DapperPaging.Offset in a FastEndpoints or API Controller handler using Dapper:

using Dapper;
using DapperPaging.Offset.Data;
using DapperPaging.Offset.Filtering;
using Paging.Common;

public class SearchPeopleEndpoint : Endpoint<PeopleSearchRequest, PagedResponse<PersonDto>>
{
    private readonly IDbConnectionFactory _dbConnectionFactory;

    public SearchPeopleEndpoint(IDbConnectionFactory dbConnectionFactory)
    {
        _dbConnectionFactory = dbConnectionFactory;
    }

    public override async Task HandleAsync(PeopleSearchRequest req, CancellationToken ct)
    {
        using var connection = _dbConnectionFactory.CreateConnection();

        // 1. Map DTO property names to Database column names if they differ
        var propertyMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
        {
            { nameof(PersonDto.Id), nameof(Person.PersonId) }
        };

        // 2. Generate SQL fragments and DynamicParameters
        var (whereClause, parameters) = req.BuildSqlWhere();
        var orderByClause = req.BuildSqlSorting(propertyMap, defaultSortColumn: "LastName");

        // 3. Add pagination parameters to the same DynamicParameters instance
        parameters.Add("Skip", req.Skip);
        parameters.Add("Take", req.PageSize);

        // 4. Combine Count and Data queries for single round-trip execution
        var sql = $@"
            SELECT COUNT(*) FROM People {whereClause};

            SELECT [PersonId] AS [Id], [FirstName], [LastName], [Email], [Age]
            FROM People
            {whereClause}
            {orderByClause}
            OFFSET @Skip ROWS
            FETCH NEXT @Take ROWS ONLY;
        ";

        // 5. Execute via QueryMultipleAsync
        using var multi = await connection.QueryMultipleAsync(sql, parameters);
        var totalCount = await multi.ReadFirstAsync<int>();
        var items = await multi.ReadAsync<PersonDto>();

        await Send.OkAsync(new PagedResponse<PersonDto>(items, totalCount, req.PageNumber, req.PageSize), ct);
    }
}

Dependencies

License

This project is licensed under the MIT License.

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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.5 89 11/9/2026
1.0.4 97 11/3/2026