I have thousands of rows, but I don't want to use just Users.ToList() probably that is not efficient. I want to send just 20 or 30 row to API each request. Next 20 rows and next 20 rows I want to do like that, is it possible ?
Like Twitter or Instagram
What is the better solution in here?
Any video or article. I need some suggestion,
Thank you to everyone
Note: currently, I am using ASP.NET Core 5.0, I am using React Native as mobile development
CodePudding user response:
Have your API endpoint accept page offset and page size parameters (how is up to you - query string, part of request body, etc). You can then use the IQueryable<T> .Skip and .Take extension methods to return the requested set of rows:
var results = _dbContext.YourDbSet
.Skip( pageOffset * pageSize )
.Take( pageSize );
CodePudding user response:
Entity Framework has built-in support for paging. You could implement it pretty easily like so:
private IQueryable<Users> GetPageOfUsers(this IQueryable<Users> query, int pageNumber, int pageSize = 20)
{
// skip pages to get to the required page number
query = query.Skip(pageSize * pageNumber);
// take the number of entities to fill this page
query = query.Take(pageSize);
return query;
}
Then, you could call this extension method like so:
Users.GetPageOfUsers(0, 20).ToList();
It is worth noting that this method doesn't account for rows that may be inserted in the middle of a page between page requests; however, for a traditional table sorted by the primary key that is database-generated and auto-incrementing, this likely isn't a material issue.
