Linq Paging - How To Enable Total Records

I'm trying to figure out how the best way to count the number of records would include paging. I need this value to figure out the total number of pages, given the page size and several other variables.
This is what I have so far that takes the start line and page size using the skip and accept instructions.

promotionInfo = (from p in matches orderby p.PROMOTION_NM descending select p).Skip(startRow).Take(pageSize).ToList(); 

I know that I can run another query, but decided that there might be another way to achieve this count without having to run the query twice.

Thanks in advance, Billy

+4
source share
1 answer

I know that I can run another query, but decided that there might be another way to achieve this count without having to run the query twice.

No, you need to run the query.

You can do something like:

 var source = from p in matches orderby p.PROMOTION_NM descending select p; var count = source.Count(); var promotionInfo = source.Skip(startRow).Take(pageSize).ToList(); 

Please note that Skip(0) not free .

+8
source

Source: https://habr.com/ru/post/1305664/


All Articles