Get the total number of records when doing pagination

前端 未结 2 381
别那么骄傲
别那么骄傲 2020-12-17 16:29

To get a page from a database I have to execute something like this:

var cs = ( from x in base.EntityDataContext.Corporates
   select x ).Skip( 10 ).Take( 10         


        
相关标签:
2条回答
  • 2020-12-17 16:39

    Bottom line: you have to run two queries. You simply can't get around it.

    Here's a good way to do it, however, that caches the original LINQ query and filter, making for less copy/paste errors:

    var qry = from x in base.EntityDataContext.Coporates select x;
    var count = qry.Count();
    var items = qry.Skip(10).Take(10).ToList();
    
    0 讨论(0)
  • 2020-12-17 16:47

    To get the total number of records before skip/take you have to run a separate query. Getting the actual number returned would use Count(), but wouldn't result in another query if the original query was materialized.

    var q = from x in base.EntityDataContext.Corporates 
            select x;
    
    var total = q.Count();
    var cs = q.Skip(10).Take(10);
    var numberOnSelectedPage = cs.Count();
    
    0 讨论(0)
提交回复
热议问题