Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 16660 | Bulk Product c5b61fcc | Books | $896.53 | 459 | ★ 2.5 | 54 | BULK-3de72db7 |
| 16661 | Bulk Product e9606d30 | Toys & Games | $789.84 | 217 | ★ 0.2 | 16 | BULK-39db35ba |
| 16662 | Bulk Product 7e1c62d7 | Electronics | $917.74 | 220 | ★ 3.0 | 82 | BULK-336dba3b |
| 16663 | Bulk Product f48c3123 | Clothing | $839.64 | 52 | ★ 0.5 | 72 | BULK-30c0cf6a |
| 16664 | Bulk Product 1e7eb8aa | Books | $163.21 | 307 | ★ 3.1 | 80 | BULK-3669c7a1 |
| 16665 | Bulk Product c642016b | Electronics | $111.39 | 271 | ★ 2.8 | 29 | BULK-d8e2b28b |
| 16666 | Bulk Product 11608499 | Electronics | $129.64 | 22 | ★ 2.3 | 22 | BULK-c89308ab |
| 16667 | Bulk Product fed9b427 | Clothing | $957.56 | 368 | ★ 3.3 | 14 | BULK-706b9e8a |
| 16668 | Bulk Product aa2b6c68 | Home & Garden | $30.65 | 32 | ★ 0.3 | 41 | BULK-4555ee5b |
| 16669 | Bulk Product 77c33cc4 | Books | $605.14 | 312 | ★ 4.1 | 4 | BULK-43ef9cc1 |
| 16670 | Bulk Product 54261010 | Electronics | $133.13 | 35 | ★ 4.2 | 73 | BULK-71a3b70b |
| 16671 | Bulk Product 258158e1 | Electronics | $659.45 | 234 | ★ 3.1 | 84 | BULK-a9c9faab |
| 16672 | Bulk Product 548cdce5 | Books | $783.71 | 393 | ★ 0.1 | 87 | BULK-edbaffef |
| 16673 | Bulk Product 881bf4eb | Home & Garden | $517.45 | 150 | ★ 0.9 | 86 | BULK-8e75bca4 |
| 16674 | Bulk Product 16bea34b | Sports & Outdoors | $449.18 | 62 | ★ 2.6 | 46 | BULK-26df29e9 |
| 16675 | Bulk Product cb52dfe2 | Toys & Games | $569.01 | 338 | ★ 3.6 | 63 | BULK-839977b8 |
| 16676 | Bulk Product 3c16b734 | Electronics | $585.23 | 437 | ★ 0.6 | 65 | BULK-27d39ec0 |
| 16677 | Bulk Product 25e56852 | Books | $223.02 | 225 | ★ 3.0 | 71 | BULK-87e4739a |
| 16678 | Bulk Product b2f305c6 | Toys & Games | $705.23 | 498 | ★ 4.3 | 51 | BULK-22c4cea3 |
| 16679 | Bulk Product 2f51e2a8 | Home & Garden | $829.38 | 86 | ★ 0.7 | 92 | BULK-2da7868c |
| 16680 | Bulk Product 0c2dfa33 | Clothing | $67.70 | 80 | ★ 4.1 | 68 | BULK-b686516b |
| 16681 | Bulk Product 8bdf3520 | Books | $408.85 | 174 | ★ 3.0 | 95 | BULK-3da734eb |
| 16682 | Bulk Product b5db39ee | Electronics | $808.83 | 92 | ★ 3.2 | 46 | BULK-dc82097e |
| 16683 | Bulk Product 5d020423 | Books | $945.29 | 478 | ★ 0.6 | 95 | BULK-b7320380 |
| 16684 | Bulk Product d093b545 | Electronics | $15.25 | 278 | ★ 4.2 | 93 | BULK-74ee74d4 |
// 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
};
}