Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 14585 | Bulk Product 6b93a80d | Toys & Games | $239.09 | 488 | ★ 0.2 | 34 | BULK-cf196a7e |
| 14586 | Bulk Product 5685690f | Sports & Outdoors | $454.89 | 379 | ★ 3.4 | 97 | BULK-4b7d7851 |
| 14587 | Bulk Product 668b59f2 | Books | $95.46 | 20 | ★ 1.5 | 81 | BULK-03a3b6bd |
| 14588 | Bulk Product 7b771204 | Home & Garden | $774.38 | 188 | ★ 3.3 | 96 | BULK-979cd63b |
| 14589 | Bulk Product 2f7f379e | Electronics | $322.06 | 136 | ★ 2.3 | 2 | BULK-05202efb |
| 14590 | Bulk Product 7079405e | Clothing | $758.77 | 340 | ★ 3.7 | 78 | BULK-deed3816 |
| 14591 | Bulk Product f270294e | Electronics | $221.66 | 265 | ★ 1.9 | 46 | BULK-ac3ecf4a |
| 14592 | Bulk Product 5797a596 | Books | $202.43 | 47 | ★ 2.0 | 7 | BULK-f3c2efa2 |
| 14593 | Bulk Product 72f2e8c8 | Home & Garden | $672.74 | 339 | ★ 4.2 | 64 | BULK-663318ef |
| 14594 | Bulk Product 1aaea52b | Home & Garden | $512.66 | 255 | ★ 4.4 | 15 | BULK-49016533 |
| 14595 | Bulk Product d46d958b | Home & Garden | $602.53 | 386 | ★ 2.8 | 5 | BULK-ea5ff83a |
| 14596 | Bulk Product a39e62d8 | Books | $855.49 | 113 | ★ 1.2 | 50 | BULK-1c1948f0 |
| 14597 | Bulk Product f53ac016 | Books | $970.87 | 143 | ★ 4.0 | 17 | BULK-02e959d8 |
| 14598 | Bulk Product 6a1ab805 | Clothing | $52.23 | 148 | ★ 0.0 | 97 | BULK-c6a1454b |
| 14599 | Bulk Product 332acb36 | Home & Garden | $443.13 | 13 | ★ 3.5 | 54 | BULK-76013e28 |
| 14600 | Bulk Product dfbbd670 | Sports & Outdoors | $114.72 | 494 | ★ 1.1 | 63 | BULK-d6bd023b |
| 14601 | Bulk Product 50d2ad44 | Electronics | $260.46 | 346 | ★ 2.1 | 78 | BULK-1f388d38 |
| 14602 | Bulk Product 67e95949 | Sports & Outdoors | $274.82 | 365 | ★ 2.5 | 59 | BULK-81bff0ef |
| 14603 | Bulk Product 2751414e | Electronics | $453.95 | 46 | ★ 1.9 | 69 | BULK-67401c08 |
| 14604 | Bulk Product 001a636c | Sports & Outdoors | $591.80 | 117 | ★ 3.8 | 29 | BULK-9aadc6b2 |
| 14605 | Bulk Product 510a1677 | Toys & Games | $208.36 | 276 | ★ 3.8 | 31 | BULK-78986d92 |
| 14606 | Bulk Product 9fcffa10 | Clothing | $806.54 | 310 | ★ 1.7 | 21 | BULK-5ee9f2cf |
| 14607 | Bulk Product a7dab9c5 | Books | $984.76 | 387 | ★ 4.7 | 71 | BULK-9a4b29c4 |
| 14608 | Bulk Product 058c2445 | Electronics | $521.09 | 351 | ★ 1.4 | 71 | BULK-4db74956 |
| 14609 | Bulk Product e51e2e2a | Clothing | $1,003.13 | 374 | ★ 3.0 | 88 | BULK-855abe08 |
// 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
};
}