Linq Paging - How to incorporate total record count

无人久伴 提交于 2020-01-01 10:12:25

问题


I am trying to figure out the best way of getting the record count will incorporating paging. I need this value to figure out the total page count given a page size and a few other variables.
This is what i have so far which takes in the starting row and the page size using the skip and take statements.

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

I know i could run another query, but figured there may be another way of achieving this count without having to run the query twice.

Thanks in advance, Billy


回答1:


I know i could run another query, but figured there may be another way of achieving this count without having to run the query twice.

No, you have 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();

Be advised, however, that Skip(0) isn't free.



来源:https://stackoverflow.com/questions/2553167/linq-paging-how-to-incorporate-total-record-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!