Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 10285 | Bulk Product bd496fe1 | Books | $821.07 | 143 | ★ 4.0 | 84 | BULK-375c4af8 |
| 10286 | Bulk Product 06c17f68 | Toys & Games | $918.17 | 108 | ★ 2.4 | 22 | BULK-1f41b98b |
| 10287 | Bulk Product 1a948566 | Toys & Games | $120.88 | 283 | ★ 3.0 | 42 | BULK-4c129380 |
| 10288 | Bulk Product ad0c0e35 | Books | $453.58 | 335 | ★ 0.3 | 5 | BULK-335cd741 |
| 10289 | Bulk Product 88c1c3fc | Clothing | $265.87 | 197 | ★ 0.0 | 6 | BULK-5e6ee0ad |
| 10290 | Bulk Product 91fa9bba | Books | $550.35 | 440 | ★ 0.2 | 48 | BULK-e136de17 |
| 10291 | Bulk Product e42981f6 | Electronics | $497.73 | 235 | ★ 2.8 | 94 | BULK-246a7c5c |
| 10292 | Bulk Product 05fbe53e | Books | $199.46 | 448 | ★ 4.7 | 78 | BULK-d8abc6a8 |
| 10293 | Bulk Product 31c9fb68 | Electronics | $101.03 | 478 | ★ 0.9 | 73 | BULK-04f29c72 |
| 10294 | Bulk Product a3f4680f | Toys & Games | $119.19 | 98 | ★ 2.0 | 28 | BULK-acef328f |
| 10295 | Bulk Product 810c98bb | Books | $151.60 | 377 | ★ 1.8 | 53 | BULK-1de6ee38 |
| 10296 | Bulk Product 79d7d132 | Clothing | $464.26 | 354 | ★ 3.7 | 5 | BULK-a69fb8d9 |
| 10297 | Bulk Product a3c8f19e | Home & Garden | $711.18 | 207 | ★ 3.5 | 85 | BULK-47311e5e |
| 10298 | Bulk Product ca2a0509 | Toys & Games | $414.47 | 135 | ★ 1.1 | 32 | BULK-04a32eb8 |
| 10299 | Bulk Product f9bad1f7 | Sports & Outdoors | $158.87 | 123 | ★ 3.5 | 44 | BULK-15b0ae5c |
| 10300 | Bulk Product 85aad116 | Toys & Games | $350.06 | 144 | ★ 4.1 | 2 | BULK-00f6c063 |
| 10301 | Bulk Product c0a3791a | Home & Garden | $926.57 | 102 | ★ 1.9 | 32 | BULK-8a8e2813 |
| 10302 | Bulk Product a87906ba | Clothing | $917.54 | 77 | ★ 1.2 | 14 | BULK-328456f1 |
| 10303 | Bulk Product 574832d6 | Sports & Outdoors | $669.33 | 373 | ★ 0.9 | 91 | BULK-8fa0d308 |
| 10304 | Bulk Product 0829c2d6 | Electronics | $312.53 | 187 | ★ 3.1 | 61 | BULK-e13abacb |
| 10305 | Bulk Product 70b6bd63 | Books | $886.51 | 422 | ★ 3.8 | 79 | BULK-3dcb4af9 |
| 10306 | Bulk Product 703d096f | Toys & Games | $388.03 | 408 | ★ 1.3 | 13 | BULK-a571fb17 |
| 10307 | Bulk Product d78cebcc | Clothing | $251.00 | 217 | ★ 4.6 | 11 | BULK-71e04981 |
| 10308 | Bulk Product d72d75f1 | Books | $230.60 | 285 | ★ 2.3 | 3 | BULK-bbbc3738 |
| 10309 | Bulk Product ab0a36dc | Clothing | $622.44 | 320 | ★ 2.5 | 39 | BULK-29883d0b |
// 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
};
}