Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 12385 | Bulk Product fab74a2c | Sports & Outdoors | $885.78 | 297 | ★ 3.5 | 49 | BULK-51b131d0 |
| 12386 | Bulk Product f4db33fa | Toys & Games | $175.38 | 431 | ★ 2.8 | 86 | BULK-d4af1fc3 |
| 12387 | Bulk Product 284ee40f | Home & Garden | $948.87 | 88 | ★ 1.0 | 78 | BULK-48780ea1 |
| 12388 | Bulk Product 6b19eec8 | Home & Garden | $940.02 | 29 | ★ 3.8 | 36 | BULK-cc335fe0 |
| 12389 | Bulk Product 8b713032 | Books | $446.32 | 286 | ★ 5.0 | 2 | BULK-2acc3e48 |
| 12390 | Bulk Product dc6ab225 | Books | $764.32 | 142 | ★ 2.7 | 36 | BULK-110dafff |
| 12391 | Bulk Product b713abde | Books | $507.52 | 145 | ★ 4.7 | 68 | BULK-0fecafb2 |
| 12392 | Bulk Product cf09e014 | Electronics | $166.50 | 248 | ★ 2.9 | 55 | BULK-f7959757 |
| 12393 | Bulk Product 4fa8bc4c | Sports & Outdoors | $168.00 | 365 | ★ 3.0 | 3 | BULK-f51b2224 |
| 12394 | Bulk Product 5d645e99 | Electronics | $53.08 | 255 | ★ 4.5 | 37 | BULK-0c40eae9 |
| 12395 | Bulk Product 13ff6ad0 | Home & Garden | $261.06 | 86 | ★ 4.6 | 34 | BULK-1ba25021 |
| 12396 | Bulk Product 63a37d63 | Electronics | $379.65 | 183 | ★ 2.1 | 76 | BULK-6ebb8673 |
| 12397 | Bulk Product 1fa25a7a | Books | $858.14 | 445 | ★ 2.5 | 60 | BULK-4c318f40 |
| 12398 | Bulk Product ba3827ee | Clothing | $10.76 | 377 | ★ 2.6 | 63 | BULK-0fc57159 |
| 12399 | Bulk Product 5e8d2c5e | Toys & Games | $619.01 | 154 | ★ 3.0 | 99 | BULK-0fb6abc3 |
| 12400 | Bulk Product e0e9b35f | Sports & Outdoors | $467.64 | 3 | ★ 4.4 | 21 | BULK-8a0eb15b |
| 12401 | Bulk Product fd832a0f | Toys & Games | $332.01 | 194 | ★ 1.7 | 80 | BULK-93b8b0b6 |
| 12402 | Bulk Product 86fbf0bc | Electronics | $36.71 | 462 | ★ 4.3 | 42 | BULK-8a3a6403 |
| 12403 | Bulk Product df8791db | Toys & Games | $424.22 | 258 | ★ 4.9 | 21 | BULK-ff100c44 |
| 12404 | Bulk Product cc299990 | Clothing | $686.43 | 4 | ★ 1.3 | 78 | BULK-c98d1748 |
| 12405 | Bulk Product c7a97a45 | Electronics | $638.49 | 384 | ★ 2.3 | 52 | BULK-a71e65d2 |
| 12406 | Bulk Product ce4fa4a5 | Toys & Games | $930.32 | 263 | ★ 4.0 | 15 | BULK-3b95f9a8 |
| 12407 | Bulk Product 1703493d | Sports & Outdoors | $600.93 | 369 | ★ 0.1 | 28 | BULK-1315b599 |
| 12408 | Bulk Product 2cf87a10 | Electronics | $478.61 | 401 | ★ 3.0 | 67 | BULK-1cb0f465 |
| 12409 | Bulk Product c78050f5 | Sports & Outdoors | $530.30 | 126 | ★ 1.9 | 70 | BULK-c9604110 |
// 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
};
}