Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 8435 | Bulk Product 201075ac | Clothing | $269.24 | 66 | ★ 2.0 | 53 | BULK-48d60632 |
| 8436 | Bulk Product b9d34b4d | Sports & Outdoors | $443.73 | 489 | ★ 1.4 | 21 | BULK-e8c2f762 |
| 8437 | Bulk Product 85a2e19d | Books | $860.97 | 221 | ★ 0.8 | 28 | BULK-0ba0bf39 |
| 8438 | Bulk Product b2e43b0b | Books | $582.22 | 443 | ★ 4.0 | 2 | BULK-9e79ceb8 |
| 8439 | Bulk Product 821973fe | Books | $601.27 | 416 | ★ 1.5 | 4 | BULK-1e5427aa |
| 8440 | Bulk Product 6da63cfa | Home & Garden | $244.39 | 403 | ★ 4.5 | 34 | BULK-99acd301 |
| 8441 | Bulk Product 6c0566fb | Clothing | $408.88 | 411 | ★ 2.3 | 14 | BULK-e05ea1b0 |
| 8442 | Bulk Product d6e674e8 | Books | $593.05 | 143 | ★ 3.9 | 15 | BULK-66cd0e84 |
| 8443 | Bulk Product 7a639f80 | Electronics | $345.85 | 244 | ★ 4.6 | 41 | BULK-cadbde36 |
| 8444 | Bulk Product 82693dc6 | Toys & Games | $906.23 | 316 | ★ 3.4 | 85 | BULK-35494946 |
| 8445 | Bulk Product ab26065a | Books | $863.44 | 441 | ★ 1.3 | 90 | BULK-aca836f5 |
| 8446 | Bulk Product c0004bb4 | Books | $156.52 | 20 | ★ 2.0 | 82 | BULK-d7b1b826 |
| 8447 | Bulk Product 4f237186 | Clothing | $77.87 | 166 | ★ 3.1 | 34 | BULK-f670713c |
| 8448 | Bulk Product 13158210 | Toys & Games | $548.83 | 211 | ★ 3.1 | 12 | BULK-09cf4e73 |
| 8449 | Bulk Product a5d8555c | Home & Garden | $126.48 | 454 | ★ 1.1 | 18 | BULK-ab56f0d3 |
| 8450 | Bulk Product 8a3cbee6 | Books | $102.43 | 218 | ★ 4.9 | 67 | BULK-8ebe827e |
| 8451 | Bulk Product 5be17c0f | Clothing | $644.70 | 351 | ★ 2.6 | 79 | BULK-bbb13d45 |
| 8452 | Bulk Product aac23f9f | Clothing | $202.50 | 259 | ★ 3.2 | 89 | BULK-01d119fb |
| 8453 | Bulk Product c9e5465a | Electronics | $476.74 | 423 | ★ 2.7 | 10 | BULK-abe2d437 |
| 8454 | Bulk Product e3721393 | Sports & Outdoors | $141.53 | 327 | ★ 1.8 | 52 | BULK-848f24a8 |
| 8455 | Bulk Product 205e326f | Clothing | $1,007.56 | 348 | ★ 2.1 | 92 | BULK-3f01148c |
| 8456 | Bulk Product c799ded7 | Home & Garden | $965.78 | 451 | ★ 3.7 | 10 | BULK-7a77f9f5 |
| 8457 | Bulk Product 5b43fffc | Books | $305.85 | 256 | ★ 3.8 | 70 | BULK-dd80fc0f |
| 8458 | Bulk Product 00615537 | Clothing | $242.92 | 398 | ★ 1.9 | 34 | BULK-0f6c10f1 |
| 8459 | Bulk Product 237e4e5b | Toys & Games | $239.14 | 251 | ★ 3.9 | 79 | BULK-23ed68f9 |
// Efficient pagination with Skip and Take
public async Task<PaginatedResult<DemoProduct>> GetPaginatedProductsAsync(int page, int pageSize)
{
// Get total count (cached if possible)
var totalCount = await _context.DemoProducts.CountAsync(p => p.IsActive);
// Get page of results
var items = await _context.DemoProducts
.Where(p => p.IsActive)
.OrderBy(p => p.Id) // Important: Must have ORDER BY for consistent paging
.Skip((page - 1) * pageSize) // OFFSET in SQL
.Take(pageSize) // FETCH NEXT in SQL
.AsNoTracking() // No tracking needed for read-only
.ToListAsync();
return new PaginatedResult<DemoProduct>
{
Items = items,
TotalCount = totalCount,
Page = page,
PageSize = pageSize
};
}