Skip to content

Instantly share code, notes, and snippets.

@Himura2la
Last active September 5, 2023 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Himura2la/1cdfab0722ff8f2a3f2471d1b15d8db0 to your computer and use it in GitHub Desktop.
Save Himura2la/1cdfab0722ff8f2a3f2471d1b15d8db0 to your computer and use it in GitHub Desktop.
Pagination for EF queries
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