I am trying to paginate Products
from MySQL db, but if I use Skip()
or Take()
it returns an empty Json array as my web api response like this
[]
But extension methods such as FirstOrDefault()
, Where()
... works fine. Here's the code snippet:
public IActionResult GetPage(int page, int pageSize = 2)
{
int productCount = _context.Products.Count(); // 5
float totalPages = (float)Math.Ceiling((float)productCount / pageSize); //2.5 -- round to 3
if (page < 1 || page > totalPages) return NotFound();
var products = _context.Products.Skip((page - 1) * pageSize).Take(pageSize); //skip & take err mysql ef
return Ok(products);
}
I even hardcoded the query .Skip(1).Take(2)
with no luck. Anyone have faced this problem or know a workaround?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…