Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 21210 | Bulk Product a5d19941 | Toys & Games | $433.21 | 478 | ★ 2.3 | 50 | BULK-d5c3e9bb |
| 21211 | Bulk Product ef5fb561 | Clothing | $670.95 | 396 | ★ 4.1 | 15 | BULK-17308ff0 |
| 21212 | Bulk Product 103f25c8 | Home & Garden | $915.15 | 276 | ★ 4.9 | 83 | BULK-b7c99d1d |
| 21213 | Bulk Product e51f3610 | Clothing | $578.57 | 482 | ★ 4.1 | 99 | BULK-ad14b3e6 |
| 21214 | Bulk Product 4015749f | Home & Garden | $105.74 | 346 | ★ 4.1 | 68 | BULK-0e8b9a0d |
| 21215 | Bulk Product d1c9e0b7 | Books | $931.30 | 432 | ★ 2.8 | 94 | BULK-c7b446de |
| 21216 | Bulk Product 0231ad1a | Books | $596.84 | 451 | ★ 1.3 | 21 | BULK-58f7fa1a |
| 21217 | Bulk Product 423588a4 | Toys & Games | $181.75 | 199 | ★ 3.4 | 39 | BULK-2ec1a520 |
| 21218 | Bulk Product 27af1c93 | Books | $734.37 | 116 | ★ 1.6 | 49 | BULK-38d86578 |
| 21219 | Bulk Product 0941dca9 | Toys & Games | $634.85 | 248 | ★ 0.5 | 25 | BULK-ea3a13b2 |
| 21220 | Bulk Product 2efdbad5 | Home & Garden | $284.28 | 100 | ★ 3.4 | 1 | BULK-dfff91a4 |
| 21221 | Bulk Product 9b834e6b | Sports & Outdoors | $438.08 | 176 | ★ 0.1 | 65 | BULK-532f0d2d |
| 21222 | Bulk Product 1e8dfbc3 | Home & Garden | $654.02 | 247 | ★ 2.5 | 1 | BULK-235866f1 |
| 21223 | Bulk Product 5a603922 | Toys & Games | $297.42 | 379 | ★ 4.4 | 86 | BULK-6fa7edcb |
| 21224 | Bulk Product 219f452d | Toys & Games | $550.01 | 459 | ★ 3.2 | 79 | BULK-0edb06a0 |
| 21225 | Bulk Product 8703d0a0 | Toys & Games | $829.95 | 110 | ★ 1.7 | 61 | BULK-d181e134 |
| 21226 | Bulk Product 9cf1d656 | Books | $598.92 | 305 | ★ 3.7 | 33 | BULK-80816320 |
| 21227 | Bulk Product d5c14c8a | Sports & Outdoors | $913.53 | 389 | ★ 2.2 | 94 | BULK-16f6c623 |
| 21228 | Bulk Product 87654c7f | Clothing | $162.85 | 34 | ★ 0.5 | 63 | BULK-b52c19a5 |
| 21229 | Bulk Product efc1ec21 | Toys & Games | $744.29 | 466 | ★ 0.8 | 42 | BULK-4602d43c |
| 21230 | Bulk Product d6fa859e | Electronics | $458.50 | 448 | ★ 1.4 | 8 | BULK-bba72ac8 |
| 21231 | Bulk Product 76d97037 | Books | $355.54 | 488 | ★ 1.0 | 99 | BULK-3834eb22 |
| 21232 | Bulk Product ffb53f64 | Electronics | $691.60 | 417 | ★ 3.4 | 77 | BULK-9da67bc1 |
| 21233 | Bulk Product a6b7a9b3 | Toys & Games | $266.69 | 88 | ★ 1.2 | 17 | BULK-99a893c2 |
| 21234 | Bulk Product 4e403866 | Toys & Games | $815.16 | 414 | ★ 3.2 | 55 | BULK-39895198 |
// 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
};
}