Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 17460 | Bulk Product 621673cf | Home & Garden | $26.43 | 170 | ★ 3.9 | 87 | BULK-1bab31a1 |
| 17461 | Bulk Product aa7993f7 | Books | $335.55 | 225 | ★ 3.0 | 51 | BULK-fec3790c |
| 17462 | Bulk Product e52e3046 | Electronics | $974.98 | 326 | ★ 0.4 | 19 | BULK-93ca459d |
| 17463 | Bulk Product 2966a2f2 | Books | $278.38 | 369 | ★ 4.2 | 21 | BULK-63331fb4 |
| 17464 | Bulk Product 00f96cba | Sports & Outdoors | $431.86 | 205 | ★ 0.3 | 30 | BULK-774c06e9 |
| 17465 | Bulk Product b935fbca | Home & Garden | $117.48 | 178 | ★ 2.2 | 9 | BULK-b691c838 |
| 17466 | Bulk Product 305c4882 | Books | $623.77 | 451 | ★ 0.9 | 53 | BULK-e8e03e02 |
| 17467 | Bulk Product c0370525 | Books | $403.46 | 128 | ★ 1.0 | 85 | BULK-7eae7d1b |
| 17468 | Bulk Product c05e6189 | Sports & Outdoors | $567.84 | 481 | ★ 3.1 | 62 | BULK-1d792df4 |
| 17469 | Bulk Product f8d333c6 | Clothing | $330.73 | 289 | ★ 3.1 | 96 | BULK-f0fb2638 |
| 17470 | Bulk Product 3ad1ed7f | Electronics | $814.30 | 242 | ★ 2.0 | 9 | BULK-21e4a900 |
| 17471 | Bulk Product 7003c1b2 | Electronics | $841.85 | 261 | ★ 1.9 | 32 | BULK-5684a57c |
| 17472 | Bulk Product 0c469984 | Books | $886.35 | 86 | ★ 4.8 | 74 | BULK-27b0e85c |
| 17473 | Bulk Product 39f6c619 | Sports & Outdoors | $955.69 | 119 | ★ 4.9 | 55 | BULK-2728164f |
| 17474 | Bulk Product 226999ce | Clothing | $36.59 | 265 | ★ 0.9 | 66 | BULK-e8dc940d |
| 17475 | Bulk Product d381954e | Sports & Outdoors | $476.31 | 469 | ★ 0.7 | 82 | BULK-4f273d9f |
| 17476 | Bulk Product c2dd41ee | Books | $337.76 | 88 | ★ 2.8 | 26 | BULK-5f5f350c |
| 17477 | Bulk Product d4fa7d16 | Electronics | $439.99 | 234 | ★ 0.2 | 32 | BULK-47159179 |
| 17478 | Bulk Product 93ff0d34 | Books | $415.49 | 404 | ★ 3.0 | 8 | BULK-5b19605b |
| 17479 | Bulk Product 552f8336 | Books | $857.20 | 222 | ★ 3.3 | 84 | BULK-34a18b65 |
| 17480 | Bulk Product 4b99bac6 | Home & Garden | $977.54 | 331 | ★ 0.0 | 19 | BULK-e569037d |
| 17481 | Bulk Product 24e66e61 | Sports & Outdoors | $261.91 | 156 | ★ 3.8 | 89 | BULK-8f01ba8b |
| 17482 | Bulk Product 7ce59b43 | Books | $78.82 | 360 | ★ 2.6 | 12 | BULK-6c60b6d8 |
| 17483 | Bulk Product 5920e3e3 | Books | $15.91 | 213 | ★ 3.8 | 1 | BULK-604cfed3 |
| 17484 | Bulk Product 8b006182 | Sports & Outdoors | $540.86 | 457 | ★ 1.6 | 36 | BULK-cb6835c5 |
// 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
};
}