0

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?

3
  • 2
    Bit confusing, can you explain with an example? Aug 30, 2016 at 6:57
  • With "total page", do you mean the number of the last page? e.g. "displaying page 1 of 14" and you want to get the "14"? Aug 30, 2016 at 6:58
  • @GeorgPatscheider yes sir that is what i mean sorry for bad english Aug 30, 2016 at 9:51

2 Answers 2

3

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)));
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.