Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 15360 | Bulk Product 30907438 | Books | $840.27 | 414 | ★ 1.2 | 12 | BULK-464ec273 |
| 15361 | Bulk Product e93535e9 | Sports & Outdoors | $248.29 | 138 | ★ 2.2 | 81 | BULK-8e706893 |
| 15362 | Bulk Product d33d39d3 | Books | $343.63 | 387 | ★ 2.4 | 20 | BULK-e1dc2477 |
| 15363 | Bulk Product ea9b713a | Books | $943.69 | 10 | ★ 2.3 | 67 | BULK-78e72bc5 |
| 15364 | Bulk Product 8f47d9b1 | Sports & Outdoors | $60.32 | 304 | ★ 3.4 | 73 | BULK-7e9f17d7 |
| 15365 | Bulk Product a6a2b53d | Clothing | $641.17 | 188 | ★ 0.4 | 88 | BULK-3ddf6cbe |
| 15366 | Bulk Product 91bfc514 | Clothing | $770.30 | 438 | ★ 2.5 | 15 | BULK-32031bb3 |
| 15367 | Bulk Product 244bc963 | Electronics | $316.13 | 404 | ★ 1.2 | 84 | BULK-9ed21a7b |
| 15368 | Bulk Product 146260b5 | Home & Garden | $532.28 | 279 | ★ 3.8 | 32 | BULK-dbae15c4 |
| 15369 | Bulk Product a5337156 | Sports & Outdoors | $881.97 | 379 | ★ 0.9 | 57 | BULK-addde117 |
| 15370 | Bulk Product 6f88a6a3 | Sports & Outdoors | $881.51 | 247 | ★ 4.2 | 22 | BULK-c7b2540b |
| 15371 | Bulk Product 8b892e61 | Electronics | $27.18 | 9 | ★ 2.4 | 24 | BULK-2825c4c8 |
| 15372 | Bulk Product d9f17d70 | Home & Garden | $243.73 | 380 | ★ 0.6 | 21 | BULK-684fd82d |
| 15373 | Bulk Product 2741db9c | Electronics | $327.77 | 364 | ★ 3.9 | 96 | BULK-e9e7a1a9 |
| 15374 | Bulk Product a0556627 | Toys & Games | $269.66 | 257 | ★ 2.0 | 11 | BULK-fedb70a8 |
| 15375 | Bulk Product 510c6032 | Electronics | $750.13 | 271 | ★ 3.9 | 79 | BULK-ed8031f1 |
| 15376 | Bulk Product 500a8897 | Home & Garden | $807.90 | 194 | ★ 1.5 | 88 | BULK-bd1adac2 |
| 15377 | Bulk Product f5b28abb | Clothing | $807.55 | 328 | ★ 3.5 | 29 | BULK-750d2088 |
| 15378 | Bulk Product e99989ba | Toys & Games | $40.88 | 309 | ★ 2.6 | 75 | BULK-f7a55527 |
| 15379 | Bulk Product f48805ab | Clothing | $698.21 | 129 | ★ 3.6 | 86 | BULK-ec5ba211 |
| 15380 | Bulk Product bdb56c40 | Sports & Outdoors | $828.69 | 235 | ★ 2.3 | 37 | BULK-622b9124 |
| 15381 | Bulk Product 77018be6 | Toys & Games | $1,009.65 | 419 | ★ 4.0 | 85 | BULK-f8b472cf |
| 15382 | Bulk Product 5aff719e | Clothing | $467.55 | 196 | ★ 1.2 | 61 | BULK-5fa4d358 |
| 15383 | Bulk Product cd5a144b | Clothing | $891.16 | 266 | ★ 0.2 | 87 | BULK-2600ec20 |
| 15384 | Bulk Product c5e64993 | Home & Garden | $280.56 | 369 | ★ 2.1 | 51 | BULK-982ac631 |
// 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
};
}