开发者

linq paging - get total rows

开发者 https://www.devze.com 2022-12-11 15:44 出处:网络
i have a question about linq. I\'m using Skip and Take开发者_如何学Go to do paging: (from l in db.ProductList

i have a question about linq. I'm using Skip and Take开发者_如何学Go to do paging:

(from l in db.ProductList
          select l).Skip((page - 1) * row_per_page).Take(row_per_page)

It work, and i need retrieve total rows of product list to calculate max page. I think i must use another query to count rows but have another way to do this in one query above?


To get a count of the rows, use the .Count() extension method

var count = (from l in db.ProductList select l).Count();


The bottom line is you will have to enumerate all items to get the total page number. But you would like to enumerate them only once. I suggest you try GroupBy method to get a IEnumerable> "pages". Then you can use pages.Count() to get page count. Or you can enumerate pages to get each page of items. The GroupBy query will be only executed once.


There isn't a way to get both the total number of rows and a "skip/take" subset of those rows in the same query.

Is there a reason that you need this in two queries?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号