Last active
September 5, 2023 12:59
-
-
Save Himura2la/1cdfab0722ff8f2a3f2471d1b15d8db0 to your computer and use it in GitHub Desktop.
Pagination for EF queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int batchSize = 50; | |
async Task Paginate<TEntity>(DbSet<TEntity> dbSet, | |
Func<int, CancellationToken, Task> action, | |
CancellationToken cancellationToken = default) where TEntity : class { | |
var count = await dbSet.CountAsync(cancellationToken); | |
var remain = count; | |
while(remain > 0) { | |
logger.LogInformation($"Processing {batchSize} of {remain} items..."); | |
await action(skip: count - remain, cancellationToken); | |
remain -= batchSize; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment