Reputation: 9443
Based on this answer on how to apply paging on a list.
I would like to extend this question of how can we get the total page of a list with a specific total collection of item per page?
For example, suppose i have 50 items each page has 12 items. How can i get the total page?
Upvotes: 0
Views: 3405
Reputation: 2814
You will need to know upfront how many records you have in total.
To do this simply use yourquery.Count()
. It will translate to sql to some query beginning with SELECT COUNT(*)...
and will not iterate over every single record.
Upvotes: 1
Reputation: 5155
Isn't it simple math?
Page count = Round(Total Records / Page Size)
in C#
int totalItemCount = myList.Count();
int pageSize = 10;
int pageCount = System.Convert.ToInt32(System.Math.Ceiling(totalItemCount / System.Convert.ToDouble(pageSize)));
Upvotes: 3