C# Entity Framework Pagination

前端 未结 5 1300
陌清茗
陌清茗 2020-12-09 04:33

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??

相关标签:
5条回答
  • 2020-12-09 05:11

    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 :)

    0 讨论(0)
  • 2020-12-09 05:12

    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

    0 讨论(0)
  • 2020-12-09 05:17

    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);
    
    0 讨论(0)
  • 2020-12-09 05:24

    If you need a quick solution you can use XPagedList https://github.com/dncuug/X.PagedList. XPagedList is a library that enables you to easily take an IEnumerable/IQueryable, chop it up into "pages", and grab a specific "page" by an index. For example

    var products = await _context.Products.ToPagedListAsync(pageNumber, pageSize)
    
    0 讨论(0)
  • 2020-12-09 05:30

    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).

    0 讨论(0)
提交回复
热议问题