Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 24435 | Bulk Product 33b664d2 | Toys & Games | $284.73 | 325 | ★ 0.3 | 58 | BULK-5bd5390b |
| 24436 | Bulk Product 538045cf | Electronics | $295.90 | 229 | ★ 4.6 | 19 | BULK-94138488 |
| 24437 | Bulk Product bbf82fa1 | Sports & Outdoors | $847.66 | 194 | ★ 0.6 | 41 | BULK-d27236c3 |
| 24438 | Bulk Product 9eae0037 | Home & Garden | $440.79 | 303 | ★ 4.7 | 82 | BULK-a64b40b8 |
| 24439 | Bulk Product 90cc08bc | Home & Garden | $980.11 | 345 | ★ 0.6 | 19 | BULK-5e1725d4 |
| 24440 | Bulk Product 399b1fca | Clothing | $740.67 | 414 | ★ 1.9 | 82 | BULK-c1ded50f |
| 24441 | Bulk Product ce653b9f | Clothing | $970.64 | 148 | ★ 3.6 | 43 | BULK-29673692 |
| 24442 | Bulk Product dafa59e1 | Sports & Outdoors | $985.39 | 131 | ★ 3.2 | 86 | BULK-3ab578b6 |
| 24443 | Bulk Product a5ed1592 | Books | $891.20 | 26 | ★ 3.4 | 67 | BULK-2836bda2 |
| 24444 | Bulk Product 8fbb3f90 | Home & Garden | $947.92 | 179 | ★ 1.6 | 40 | BULK-cb372ff8 |
| 24445 | Bulk Product 6cafb54b | Sports & Outdoors | $558.05 | 346 | ★ 0.8 | 2 | BULK-daae97ea |
| 24446 | Bulk Product 10928e36 | Sports & Outdoors | $390.73 | 347 | ★ 2.1 | 33 | BULK-ac867bef |
| 24447 | Bulk Product d05210ff | Home & Garden | $301.72 | 316 | ★ 1.7 | 23 | BULK-4281c87c |
| 24448 | Bulk Product 03e7fa16 | Toys & Games | $217.49 | 351 | ★ 4.9 | 35 | BULK-b087b7c4 |
| 24449 | Bulk Product d7359314 | Toys & Games | $815.75 | 321 | ★ 1.7 | 20 | BULK-d27a9bf9 |
| 24450 | Bulk Product fef32810 | Electronics | $695.29 | 76 | ★ 0.1 | 99 | BULK-a953dd47 |
| 24451 | Bulk Product 20238c92 | Books | $1,000.75 | 300 | ★ 0.5 | 64 | BULK-98b2031f |
| 24452 | Bulk Product f39e13f8 | Toys & Games | $947.80 | 324 | ★ 2.7 | 43 | BULK-270b8157 |
| 24453 | Bulk Product 84cbaca7 | Sports & Outdoors | $312.17 | 187 | ★ 4.8 | 5 | BULK-69cf6234 |
| 24454 | Bulk Product c358c742 | Sports & Outdoors | $134.93 | 205 | ★ 4.8 | 23 | BULK-c83ee9c0 |
| 24455 | Bulk Product 77b993e4 | Sports & Outdoors | $400.77 | 309 | ★ 3.5 | 67 | BULK-aa3a1c32 |
| 24456 | Bulk Product a9ed7f6b | Electronics | $563.41 | 278 | ★ 1.9 | 60 | BULK-acc8dc1f |
| 24457 | Bulk Product c0699500 | Electronics | $546.55 | 30 | ★ 4.0 | 27 | BULK-e2fb6e7e |
| 24458 | Bulk Product 89e67dbf | Clothing | $907.86 | 108 | ★ 0.1 | 52 | BULK-6540f46b |
| 24459 | Bulk Product f20187d8 | Clothing | $670.03 | 212 | ★ 1.3 | 56 | BULK-9e0d4777 |
// 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
};
}