|
3 | 3 | [](https://github.com/tmacharia/paginator.efcore/actions/workflows/dotnet.yml)
|
4 | 4 | [](https://www.nuget.org/packages/Paginator.EntityFrameworkCore)
|
5 | 5 |
|
6 |
| -Asynchronous pagination of queries to your data store using EntityFrameworkCore. |
| 6 | +Asynchronous & Synchronous pagination of queries to your data store using EntityFrameworkCore. With the help of some simple to use extension methods, querying portions/chunks of data just gets easier. |
| 7 | + |
| 8 | +## Use Cases |
| 9 | + |
| 10 | +Suppose we have 5,000 rows in the `Employee` table, lets look at a few different ways to query chunks of that data as efficiently as possible and return the result in a paginated format. |
| 11 | + |
| 12 | +This code snippet represents our connection to the table whilst telling EF not to track the results of the query, inorder to get some slight performance improvement. |
| 13 | + |
| 14 | +```csharp |
| 15 | +IQueryable<Employee> employees = _context.Set<Employee>().AsNoTracking(); |
| 16 | +``` |
| 17 | + |
| 18 | + |
| 19 | +### Example-I: Page 2, Perpage 20 |
| 20 | +Internally translates to skipping the first page and taking the next 20 records that follow. |
| 21 | +```csharp |
| 22 | +PagedResult<Employee> paged = employees.Paginate(2,20); |
| 23 | +Console.WriteLine(paged); |
| 24 | +// Page: 2 Perpage: 20 Totalpages: 250 TotalItems: 5,000 |
| 25 | +``` |
| 26 | +### Example-II: Page 2, Perpage 20, Asynchronously |
| 27 | +`await` call to `.PaginateAsync` and optionally pass a `CancellationToken` as shown below. The result of this query will be the same as that of the previous example. |
| 28 | + |
| 29 | +An `OperationCanceledException` is thrown if cancellation is requested on the specified cancellationToken. |
| 30 | + |
| 31 | +```csharp |
| 32 | +var tokenSource = new CancellationTokenSource(); |
| 33 | + |
| 34 | +PagedResult<Employee> paged = await employees.PaginateAsync(2,20, token: tokenSource.Token); |
| 35 | + |
| 36 | +Console.WriteLine(paged); |
| 37 | +// Page: 2 Perpage: 20 Totalpages: 250 TotalItems: 5,000 |
| 38 | +``` |
| 39 | + |
| 40 | +## Skip count & perfomance concerns of `.Count()` |
| 41 | + |
| 42 | +We all know that pagination is a two step procedure, with one counting the total number of items in a sequence, and the second that picks N items from that sequence. |
| 43 | + |
| 44 | +Sometimes, the `.Count()` query doesn't perform very well especially when dealing with large datasets, or your use case might not require knowing `TotalPages` and `TotalItems`. We can tell the pagination method to ignore doing the count operation as illustrated in the examples below. |
| 45 | + |
| 46 | + |
| 47 | +### Example-III: Page 2, Perpage 20, Skipping Count |
| 48 | +To ignore count, just pass `true` as a parameter to the `param: skipCount` which comes right after perpage. |
| 49 | + |
| 50 | +Take a look at the console output to spot the difference. |
| 51 | +```csharp |
| 52 | +PagedResult<Employee> paged = employees.Paginate(2,20, skipCount: true); |
| 53 | +Console.WriteLine(paged); |
| 54 | +// Page: 2 Perpage: 20 Totalpages: 1 TotalItems: 20 |
| 55 | +``` |
| 56 | + |
| 57 | +## Contributions |
| 58 | + |
| 59 | +Suggestions & improvements are welcome. |
0 commit comments