Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 22485 | Bulk Product 0510fe14 | Home & Garden | $1,000.51 | 309 | ★ 3.3 | 44 | BULK-c73af304 |
| 22486 | Bulk Product c4449c6e | Electronics | $753.02 | 139 | ★ 0.7 | 22 | BULK-5c844d1a |
| 22487 | Bulk Product 812f8904 | Books | $979.88 | 251 | ★ 4.4 | 11 | BULK-91442d45 |
| 22488 | Bulk Product aee5aaae | Clothing | $330.09 | 396 | ★ 3.6 | 21 | BULK-e24b7d00 |
| 22489 | Bulk Product 26d59c39 | Books | $528.25 | 462 | ★ 4.1 | 50 | BULK-e00dc6d5 |
| 22490 | Bulk Product 76f1287e | Home & Garden | $316.46 | 249 | ★ 1.1 | 74 | BULK-7de0244f |
| 22491 | Bulk Product 0d78d888 | Toys & Games | $852.05 | 144 | ★ 4.9 | 81 | BULK-0da96849 |
| 22492 | Bulk Product 56c4bbd1 | Sports & Outdoors | $876.61 | 311 | ★ 3.6 | 38 | BULK-2613472f |
| 22493 | Bulk Product fe42b422 | Clothing | $484.92 | 440 | ★ 1.8 | 73 | BULK-188c23b5 |
| 22494 | Bulk Product ad611f71 | Electronics | $696.12 | 74 | ★ 1.0 | 9 | BULK-964b2a9b |
| 22495 | Bulk Product 55e46121 | Books | $531.16 | 210 | ★ 3.1 | 27 | BULK-a3515547 |
| 22496 | Bulk Product a77d60dd | Toys & Games | $919.74 | 410 | ★ 4.8 | 36 | BULK-4cd33d2f |
| 22497 | Bulk Product 637dc159 | Sports & Outdoors | $735.44 | 457 | ★ 3.4 | 48 | BULK-8999c919 |
| 22498 | Bulk Product 061a6f24 | Toys & Games | $178.83 | 497 | ★ 3.6 | 95 | BULK-5967d761 |
| 22499 | Bulk Product 48a69d22 | Books | $465.93 | 13 | ★ 3.5 | 78 | BULK-9d3b69ff |
| 22500 | Bulk Product c81bb476 | Clothing | $74.89 | 161 | ★ 2.4 | 1 | BULK-fb8109d3 |
| 22501 | Bulk Product 4ad8820f | Books | $558.31 | 205 | ★ 3.2 | 77 | BULK-d055322d |
| 22502 | Bulk Product 93a1b8a3 | Toys & Games | $905.39 | 30 | ★ 1.2 | 66 | BULK-8f13f741 |
| 22503 | Bulk Product 9479ad04 | Toys & Games | $466.13 | 55 | ★ 1.4 | 93 | BULK-13a1f260 |
| 22504 | Bulk Product de03f2b5 | Sports & Outdoors | $905.47 | 27 | ★ 1.8 | 31 | BULK-26784ee1 |
| 22505 | Bulk Product 9d64e144 | Toys & Games | $650.72 | 49 | ★ 4.7 | 79 | BULK-bae2fc55 |
| 22506 | Bulk Product 49a83c49 | Books | $988.00 | 159 | ★ 3.8 | 62 | BULK-dc6232aa |
| 22507 | Bulk Product 26d5a42a | Clothing | $939.70 | 155 | ★ 3.3 | 7 | BULK-8f9afd0f |
| 22508 | Bulk Product ed31149a | Electronics | $841.47 | 394 | ★ 1.1 | 10 | BULK-b071a21b |
| 22509 | Bulk Product 741f4454 | Books | $945.58 | 485 | ★ 0.3 | 69 | BULK-01d80ee4 |
// 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
};
}