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?

Community
  • 1
  • 1
Shift 'n Tab
  • 8,808
  • 12
  • 73
  • 117

2 Answers2

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)));
ViRuSTriNiTy
  • 5,017
  • 2
  • 32
  • 58
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.

Mauro Sampietro
  • 2,739
  • 1
  • 24
  • 50