Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 6435 | Bulk Product 3cdf7cbb | Sports & Outdoors | $975.51 | 180 | ★ 1.4 | 43 | BULK-7923f12e |
| 6436 | Bulk Product a6743347 | Clothing | $629.14 | 89 | ★ 2.0 | 58 | BULK-0aca4198 |
| 6437 | Bulk Product 787bb1b3 | Books | $32.32 | 147 | ★ 2.9 | 2 | BULK-62581c0a |
| 6438 | Bulk Product 1fae8c8b | Toys & Games | $969.19 | 156 | ★ 2.6 | 39 | BULK-dbfd4812 |
| 6439 | Bulk Product fa7559b2 | Toys & Games | $530.52 | 166 | ★ 4.0 | 13 | BULK-58cb8c13 |
| 6440 | Bulk Product 66472cb6 | Sports & Outdoors | $792.74 | 354 | ★ 3.4 | 41 | BULK-d906a1c7 |
| 6441 | Bulk Product 86db19ee | Electronics | $104.24 | 200 | ★ 4.9 | 81 | BULK-7787d2a5 |
| 6442 | Bulk Product 68bd4bd8 | Sports & Outdoors | $129.78 | 269 | ★ 4.2 | 19 | BULK-c019956a |
| 6443 | Bulk Product af484f0f | Sports & Outdoors | $434.36 | 30 | ★ 3.9 | 30 | BULK-f1a25201 |
| 6444 | Bulk Product 93759770 | Clothing | $567.51 | 182 | ★ 1.6 | 51 | BULK-4e927ce9 |
| 6445 | Bulk Product 70524502 | Books | $728.28 | 148 | ★ 0.2 | 94 | BULK-16f21feb |
| 6446 | Bulk Product 95cc4572 | Sports & Outdoors | $167.54 | 463 | ★ 2.0 | 75 | BULK-5170b839 |
| 6447 | Bulk Product 8dd3b177 | Clothing | $964.16 | 193 | ★ 0.4 | 60 | BULK-1b565d91 |
| 6448 | Bulk Product c0927625 | Books | $761.24 | 399 | ★ 1.1 | 85 | BULK-65f7fef3 |
| 6449 | Bulk Product e8278210 | Books | $363.08 | 101 | ★ 3.3 | 70 | BULK-8114d5cd |
| 6450 | Bulk Product 8a8bf09e | Clothing | $962.22 | 370 | ★ 0.2 | 43 | BULK-e5b6ba79 |
| 6451 | Bulk Product 411e31d0 | Home & Garden | $222.83 | 270 | ★ 3.0 | 80 | BULK-131a97c3 |
| 6452 | Bulk Product 54f1ac36 | Clothing | $421.64 | 135 | ★ 4.6 | 70 | BULK-ac0e9e2c |
| 6453 | Bulk Product 2a21ff4e | Sports & Outdoors | $745.10 | 188 | ★ 2.7 | 10 | BULK-6d0bc837 |
| 6454 | Bulk Product 0ff7654d | Sports & Outdoors | $307.55 | 96 | ★ 2.1 | 62 | BULK-777f5959 |
| 6455 | Bulk Product ab7101d5 | Electronics | $424.67 | 102 | ★ 0.2 | 30 | BULK-e6a4bf5b |
| 6456 | Bulk Product eaef11ab | Sports & Outdoors | $468.15 | 356 | ★ 3.1 | 77 | BULK-545144cd |
| 6457 | Bulk Product 528a3253 | Clothing | $362.69 | 410 | ★ 0.5 | 84 | BULK-26e94f95 |
| 6458 | Bulk Product 13f09407 | Clothing | $362.97 | 358 | ★ 1.7 | 67 | BULK-38ee9ad0 |
| 6459 | Bulk Product 93cb0c8a | Sports & Outdoors | $231.87 | 303 | ★ 2.5 | 65 | BULK-f1654eaf |
// 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
};
}