Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Entity Framework Pagination

Is there a way to get the row count of a complex Linq query and millions of records without hitting the db twice or writing 2 separate queries??

I might have my own suggestion. Write a stored procedure, but I'm good with MySQL not MSSQL.

Any better suggestions would be great. Also, if anyone knows if Microsoft is working on adding this feature to the entity framework.

like image 222
Jason Foglia Avatar asked Apr 13 '12 17:04

Jason Foglia


4 Answers

I'd suggest using the Take() function. This can be used to specify the number of records to take from a linq query or List. For example

List<customers> _customers = (from a in db.customers select a).ToList();
var _dataToWebPage = _customers.Take(50);

I use a similar technique in an MVC app where I write the _customers list to the session and then use this list for further pagination queries when the user clicks on page 2, 3 etc. This saves multiple database hits. However if your list is very large then writing it too the session is probably not a good idea.

For pagination you can use the Skip() and Take() function together. For example to get page 2 of the data :

var _dataToWebPage = _customers.Skip(50).Take(50);
like image 55
Tim Newton Avatar answered Sep 21 '22 02:09

Tim Newton


I was recently inspired by (copied from) this Code Project article Entity Framework pagination

The code:

public async Task<IList<SavedSearch>> FindAllSavedSearches(int page, int limit)
{
    if (page == 0)
        page = 1;

    if (limit == 0)
        limit = int.MaxValue;

    var skip = (page - 1) * limit;

    var savedSearches = _databaseContext.SavedSearches.Skip(skip).Take(limit).Include(x => x.Parameters);
    return await savedSearches.ToArrayAsync();
}

I'm not experienced with Entity Framework and I've not tested it for performance, so use with caution :)

like image 36
jk1990 Avatar answered Sep 20 '22 02:09

jk1990


The common way to show millions of records is simply not to display all pages. Think of it: if you have millions of records, say 20 or even 100 items per page, then you'll have tens of thousands of pages. It does not make sense to show them all. You can simply load the current page and provide a link to the next page, that's it. Or you may load say 100-500 records, but still show only one page and use the loaded records information to generate page links for first several pages (so know for sure how many next pages are available).

like image 20
Vladimir Perevalov Avatar answered Sep 17 '22 02:09

Vladimir Perevalov


it is so easy on sql server . you can write this query :

select count() over(), table.* from table

the count () over() will return the count of total row in result , So you don't need to run two query.. Remember that you should run raw sql on your context or use dapper that return result as view model

like image 42
S.Mohamed Mahdi Ahmadian zadeh Avatar answered Sep 18 '22 02:09

S.Mohamed Mahdi Ahmadian zadeh