Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/benbristow/entityframeworkcore.pagination

Basic Entity Framework Core Pagination implementation
https://github.com/benbristow/entityframeworkcore.pagination

entity-framework-core nuget

Last synced: about 2 months ago
JSON representation

Basic Entity Framework Core Pagination implementation

Awesome Lists containing this project

README

        

# BenBristow.EntityFrameworkCore.Pagination

[![Build/Test/Publish](https://github.com/benbristow/EntityFrameworkCore.Pagination/actions/workflows/cicd.yml/badge.svg)](https://github.com/benbristow/EntityFrameworkCore.Pagination/actions/workflows/cicd.yml)

This library provides an easy-to-use extension for paginating `IQueryable` objects in Entity Framework Core, allowing
for efficient, dynamic pagination of database queries.

## Getting Started

### Installation

To install the library, use the following NuGet command:

```bash
dotnet add package BenBristow.EntityFrameworkCore.Pagination
```

### Basic Usage

1. **Reference the library** in your project by adding `using BenBristow.EntityFrameworkCore.Pagination.Extensions;`.

2. **Paginate your query** by calling `ToPaginationResultAsync` on any `IOrderedQueryable` object, specifying the
desired page and page size.

Example:

```csharp
var paginatedResults = await myDbContext.MyEntities
.OrderBy(entity => entity.Id)
.ToPaginationResultAsync(page: 1, pageSize: 10);
```

### Usage with Projection

You can also use projection to select specific fields or transform the results while paginating:

```csharp
var paginatedResults = await myDbContext.MyEntities
.OrderBy(entity => entity.Id)
.ToPaginationResultAsync(
entity => new MyDto { Id = entity.Id, Name = entity.Name },
page: 1,
pageSize: 10
);
```

## API Reference

### `ToPaginationResultAsync`

Paginates the source `IQueryable` based on the provided parameters.

#### Parameters

- `source`: The ordered `IQueryable` source to paginate.
- `page`: The page number to retrieve. Defaults to 1.
- `pageSize`: The number of items per page. Can be `null` to return all items.
- `cancellationToken`: A token to observe while waiting for the task to complete.

#### Returns

A `Task>` representing the asynchronous operation, containing the paginated results.

### `ToPaginationResultAsync`

Paginates the source `IQueryable` and projects the results to `TResult` based on the provided parameters.

#### Parameters

- `source`: The ordered `IQueryable` source to paginate.
- `projection`: A function to map each element of `TSource` to `TResult`.
- `page`: The page number to retrieve. Defaults to 1.
- `pageSize`: The number of items per page. Can be `null` to return all items.
- `cancellationToken`: A token to observe while waiting for the task to complete.

#### Returns

A `Task>` representing the asynchronous operation, containing the paginated and projected results.

## Example

```csharp
public class MyEntityService
{
private readonly MyDbContext _context;

public MyEntityService(MyDbContext context)
{
_context = context;
}

///
/// Asynchronously gets paginated entities.
///
/// The page number to retrieve, starting from 1.
/// The number of items per page.
/// A task that represents the asynchronous operation. The task result contains the paginated results.
public async Task> GetPaginatedEntities(int page = 1, int pageSize = 10)
{
var paginatedResults = await _context.Entities
.OrderBy(e => e.Id)
.ToPaginationResultAsync(page, pageSize);

return paginatedResults;
}

///
/// Asynchronously gets paginated entities with projection to DTOs.
///
/// The page number to retrieve, starting from 1.
/// The number of items per page.
/// A task that represents the asynchronous operation. The task result contains the paginated and projected results.
public async Task> GetPaginatedEntityDtos(int page = 1, int pageSize = 10)
{
var paginatedResults = await _context.Entities
.OrderBy(e => e.Id)
.ToPaginationResultAsync(
e => new MyEntityDto { Id = e.Id, Name = e.Name },
page,
pageSize
);

return paginatedResults;
}
}
```

## Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue for any bugs or feature requests.

## License

This project is licensed under the MIT License - see the LICENSE file for details.