'How can I get next 10 or 20 row each API request with Entity Framework?

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



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John Glenn