We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Param • 3 years ago

Great article on how to implement paging with EF Core in a generic way. Any recommendations on how you would re-factor this to implement the new IAsyncEnumerable interface available in C# 8?

https://btburnett.com/cshar...

Kevin • 3 years ago

I second this question. I have been looking at how to get an asynchronous queryable but so far haven't been successful.

Paul Whitworth • 1 year ago

Great article. Worked like a charm. Thank you!

Danilo • 1 year ago

I have a question.
Do you what happens if during a "navigation" through pages updates are made to that table?
I'm using similar pagination here to iterate through all rows of a table and do such a data processing.
It seems that some rows were not taken. I think they are missing cause a lot of inserts happened in the middle of process and messed the iterator.

talynone • 3 years ago

Thanks, here's an async version for those that may find it useful:



public static async Task<pagedresult<t>> GetPagedAsync<t>(this IQueryable<t> query,
int page, int pageSize) where T : class
{
var result = new PagedResult<t>();
result.CurrentPage = page;
result.PageSize = pageSize;
result.RowCount = await query.CountAsync();


var pageCount = (double)result.RowCount / pageSize;
result.PageCount = (int)Math.Ceiling(pageCount);

var skip = (page - 1) * pageSize;


result.Results = await query.Skip(skip).Take(pageSize).ToListAsync();

return result;
}

Ricardo Cardoso • 4 years ago

I have a doubt. Why does RowCount = query.Count () not run JOINS?
This brings a total amount of rows different from the results results.
Because without Joins RowCount is unlike Results?

Tylord • 4 years ago

Nice, but can you please explain how to set the modle on the view

eight05 • 4 years ago

Is there any way you could clarify where in the project you've added these files? Also, I'm getting a type mismatch between my controller and my view. Can you include the @model statement you are using in your view? You have the line: @foreach (var contact in Model.Results) but I don't see where Model is declared. Thank you!

Harsha Serasinghe • 4 years ago

This generic pagination solution is really handy and easily integrated into any project.

sdsd • 4 years ago

thanks for wasting my 3 hours!!!!! i am sure it works with little tweaks her e and there but I couldnt make it!!!! it doesnt work as it is!!!!

Anonymous • 4 years ago

It works as is. This should serve as a good foundation. Perhaps posting your issues, the author (or others) might be willing to help. However, coming out "guns blazing" isn't going to garner you much favor in that regard...

Anonymous • 5 years ago

Thank you for the tutorial, it worked like a charm.

Elizey Ahmad • 5 years ago

Hi Thanks for the tutorial but I am getting error at GetPaged<t> and I dont know anything about this. Can you please complete source code ?
Thanks

Tin Le Van • 4 years ago

public static class PageQuery
{
public static PagedResult<t> GetPaged<t>(this IQueryable<t> query,
int page, int pageSize) where T : class
{
var result = new PagedResult<t>();
result.CurrentPage = page;
result.PageSize = pageSize;
result.RowCount = query.Count();

var pageCount = (double)result.RowCount / pageSize;
result.PageCount = (int)Math.Ceiling(pageCount);

var skip = (page - 1) * pageSize;
result.Results = query.Skip(skip).Take(pageSize).ToList();

return result;
}
}

Curtis Newton • 3 years ago

it does not work
GetPaged is not part of a dbset

Curtis Newton • 3 years ago

ok if you dont reply, I wont do it that way
it just does not wwork