Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 17610 | Bulk Product 418abe04 | Books | $487.45 | 436 | ★ 2.3 | 84 | BULK-0d7515ca |
| 17611 | Bulk Product dbc36e8c | Clothing | $131.23 | 304 | ★ 4.1 | 6 | BULK-db971d33 |
| 17612 | Bulk Product 27c7e7b5 | Sports & Outdoors | $848.66 | 476 | ★ 3.9 | 32 | BULK-3bcb6125 |
| 17613 | Bulk Product 7659bda5 | Clothing | $391.43 | 95 | ★ 2.6 | 63 | BULK-842bd302 |
| 17614 | Bulk Product 53619bdc | Home & Garden | $732.12 | 406 | ★ 0.3 | 35 | BULK-8b23fcd5 |
| 17615 | Bulk Product 3b372bf7 | Sports & Outdoors | $366.71 | 407 | ★ 2.1 | 87 | BULK-35936858 |
| 17616 | Bulk Product 8d21d080 | Electronics | $299.81 | 101 | ★ 0.0 | 24 | BULK-4856d202 |
| 17617 | Bulk Product fa9866c2 | Books | $155.43 | 116 | ★ 2.9 | 11 | BULK-fb27eade |
| 17618 | Bulk Product 37aaa1b0 | Clothing | $559.35 | 150 | ★ 1.0 | 40 | BULK-88cf4c1e |
| 17619 | Bulk Product 6f87f036 | Sports & Outdoors | $600.96 | 415 | ★ 3.6 | 1 | BULK-6fb7c8f1 |
| 17620 | Bulk Product 4dc2e5da | Toys & Games | $231.16 | 149 | ★ 3.1 | 96 | BULK-897d3fb1 |
| 17621 | Bulk Product 4b7d9ade | Books | $290.36 | 284 | ★ 2.8 | 16 | BULK-7f76d1b0 |
| 17622 | Bulk Product 92dddc79 | Electronics | $973.44 | 392 | ★ 0.4 | 10 | BULK-99c06736 |
| 17623 | Bulk Product 41d40c32 | Clothing | $262.71 | 485 | ★ 4.8 | 31 | BULK-e7abc149 |
| 17624 | Bulk Product 378717bc | Sports & Outdoors | $967.49 | 111 | ★ 3.7 | 49 | BULK-47025d93 |
| 17625 | Bulk Product 2d16a568 | Sports & Outdoors | $27.92 | 134 | ★ 4.1 | 18 | BULK-6aa0ee69 |
| 17626 | Bulk Product 8204df2e | Home & Garden | $881.84 | 413 | ★ 0.0 | 49 | BULK-1d49e673 |
| 17627 | Bulk Product 7fa4d05e | Home & Garden | $816.29 | 122 | ★ 1.4 | 59 | BULK-12e1baa9 |
| 17628 | Bulk Product 4d0df00a | Sports & Outdoors | $705.61 | 72 | ★ 2.4 | 3 | BULK-0f7bb441 |
| 17629 | Bulk Product 3ad404fb | Electronics | $101.41 | 253 | ★ 0.3 | 66 | BULK-02f19185 |
| 17630 | Bulk Product bcc848ba | Home & Garden | $334.31 | 380 | ★ 4.3 | 91 | BULK-16710c19 |
| 17631 | Bulk Product 24be5d0f | Sports & Outdoors | $762.79 | 9 | ★ 0.5 | 94 | BULK-dd4715a6 |
| 17632 | Bulk Product 743fd289 | Clothing | $415.84 | 145 | ★ 2.5 | 58 | BULK-94701453 |
| 17633 | Bulk Product 3a604952 | Clothing | $219.18 | 407 | ★ 0.8 | 33 | BULK-6e1cd487 |
| 17634 | Bulk Product 2a3a7103 | Clothing | $240.78 | 179 | ★ 4.4 | 50 | BULK-c1597a09 |
// 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
};
}