Entity Framework Core: how to get records from start index x with limit of y?

c# entity-framework-core

Question

Example

  • Page 1: start index 0, limit 50
  • Page 2: start index 49, limit 50
  • Page n: start index x, limit y

Is this right?

context.Products.Where(condition).Skip(x).Take(y);
1
-2
8/9/2017 12:38:38 AM

Accepted Answer

To get :

  • 1st page you need to skip 0 and take 50
  • 2nd page you need to skip 50 and take 50
  • ...
  • Nth page you need to skip (N-1) * 50 and take 50

So the correct syntax will be:

// pageIndex => the page index. page starting at 1.
// pageSize => the page size.
context.Products.Where(condition).Skip((pageIndex - 1) * pageSize).Take(pageSize);

If you page start at 0, so you will do:

context.Products.Where(condition).Skip(pageIndex * pageSize).Take(pageSize);
3
8/9/2017 12:35:46 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow