Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 11110 | Bulk Product 00a2467e | Toys & Games | $849.43 | 302 | ★ 0.2 | 86 | BULK-2c086b3c |
| 11111 | Bulk Product ed2248e7 | Electronics | $532.23 | 249 | ★ 1.4 | 22 | BULK-39d47927 |
| 11112 | Bulk Product 8f88758b | Books | $759.01 | 196 | ★ 2.2 | 65 | BULK-65c616c8 |
| 11113 | Bulk Product f4bdda94 | Home & Garden | $92.75 | 84 | ★ 0.2 | 57 | BULK-c9b0d4aa |
| 11114 | Bulk Product 144b404a | Home & Garden | $756.16 | 313 | ★ 2.6 | 40 | BULK-5e05d8b5 |
| 11115 | Bulk Product 8c2bedc9 | Home & Garden | $272.71 | 138 | ★ 4.0 | 95 | BULK-c59dd1c3 |
| 11116 | Bulk Product 21ef2140 | Books | $262.82 | 153 | ★ 1.1 | 62 | BULK-6bd18253 |
| 11117 | Bulk Product 2ce40835 | Books | $474.89 | 480 | ★ 2.6 | 81 | BULK-a567b278 |
| 11118 | Bulk Product 81b165be | Books | $910.03 | 378 | ★ 1.0 | 86 | BULK-a6300041 |
| 11119 | Bulk Product fdfd6d75 | Home & Garden | $239.10 | 261 | ★ 1.0 | 74 | BULK-d5b66d26 |
| 11120 | Bulk Product 111932ac | Electronics | $55.05 | 219 | ★ 4.9 | 24 | BULK-a3489aaf |
| 11121 | Bulk Product 7031e20c | Electronics | $153.44 | 72 | ★ 0.9 | 12 | BULK-f023f46d |
| 11122 | Bulk Product 0a18c5ef | Toys & Games | $189.04 | 170 | ★ 3.1 | 69 | BULK-5f6d2048 |
| 11123 | Bulk Product 9633aca1 | Sports & Outdoors | $791.08 | 8 | ★ 4.3 | 33 | BULK-13417bd5 |
| 11124 | Bulk Product 9d3a1976 | Toys & Games | $568.21 | 219 | ★ 3.3 | 65 | BULK-5eb70463 |
| 11125 | Bulk Product f78b0e38 | Home & Garden | $891.52 | 13 | ★ 4.2 | 64 | BULK-d5365369 |
| 11126 | Bulk Product 2ec204d9 | Home & Garden | $22.59 | 221 | ★ 0.0 | 83 | BULK-993f5ef8 |
| 11127 | Bulk Product d071b4ff | Home & Garden | $428.54 | 52 | ★ 3.8 | 62 | BULK-48ce36fd |
| 11128 | Bulk Product 0995e02a | Toys & Games | $105.84 | 271 | ★ 4.0 | 5 | BULK-78507c30 |
| 11129 | Bulk Product 339a58ee | Electronics | $762.61 | 443 | ★ 4.1 | 12 | BULK-248c4dc0 |
| 11130 | Bulk Product 4ccfc55b | Home & Garden | $345.91 | 486 | ★ 4.3 | 95 | BULK-4051619c |
| 11131 | Bulk Product 2e75ec2a | Electronics | $834.58 | 22 | ★ 3.3 | 13 | BULK-96cae598 |
| 11132 | Bulk Product 871f4078 | Electronics | $557.85 | 429 | ★ 2.4 | 25 | BULK-6ebb0a9b |
| 11133 | Bulk Product b4262e5c | Books | $429.41 | 244 | ★ 2.1 | 13 | BULK-e8e33210 |
| 11134 | Bulk Product d8a49458 | Toys & Games | $87.86 | 250 | ★ 1.9 | 47 | BULK-ce67a1ed |
// 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
};
}