Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 24635 | Bulk Product 2892f7bc | Home & Garden | $478.51 | 365 | ★ 0.2 | 55 | BULK-5982ab5a |
| 24636 | Bulk Product e4f5f51b | Books | $834.86 | 403 | ★ 2.1 | 70 | BULK-c4e74f3e |
| 24637 | Bulk Product 85bbf799 | Clothing | $884.43 | 304 | ★ 4.4 | 56 | BULK-ece144ae |
| 24638 | Bulk Product a5b25169 | Books | $697.22 | 457 | ★ 0.2 | 31 | BULK-fa4ec5d0 |
| 24639 | Bulk Product 3b99d163 | Toys & Games | $353.75 | 424 | ★ 3.1 | 32 | BULK-f0897f3d |
| 24640 | Bulk Product c5da991b | Electronics | $505.47 | 450 | ★ 3.0 | 41 | BULK-9ad9c4ad |
| 24641 | Bulk Product a3a258a5 | Electronics | $55.59 | 114 | ★ 1.7 | 80 | BULK-67745a6f |
| 24642 | Bulk Product a3e3ddad | Clothing | $312.61 | 388 | ★ 0.3 | 51 | BULK-d4b5b6e8 |
| 24643 | Bulk Product f3b0afea | Sports & Outdoors | $835.60 | 288 | ★ 4.0 | 33 | BULK-5d92c274 |
| 24644 | Bulk Product bf7d2fd0 | Sports & Outdoors | $959.05 | 330 | ★ 3.4 | 46 | BULK-783835d0 |
| 24645 | Bulk Product f4901d3a | Toys & Games | $712.67 | 49 | ★ 2.8 | 50 | BULK-b9b7efc4 |
| 24646 | Bulk Product 9fd5aab3 | Books | $136.62 | 2 | ★ 2.2 | 90 | BULK-cac0f38b |
| 24647 | Bulk Product 90fdc77c | Electronics | $100.19 | 371 | ★ 1.6 | 15 | BULK-320016a4 |
| 24648 | Bulk Product 65070673 | Electronics | $241.43 | 323 | ★ 1.6 | 74 | BULK-9b9c6b12 |
| 24649 | Bulk Product 01b04176 | Toys & Games | $275.67 | 57 | ★ 1.0 | 50 | BULK-43bb9b07 |
| 24650 | Bulk Product b504e73b | Electronics | $151.05 | 357 | ★ 3.7 | 58 | BULK-e2e4d39b |
| 24651 | Bulk Product 6f4dd0fd | Electronics | $570.33 | 355 | ★ 3.4 | 37 | BULK-ff06842a |
| 24652 | Bulk Product 537437da | Toys & Games | $443.08 | 291 | ★ 2.9 | 44 | BULK-b6fd0541 |
| 24653 | Bulk Product 3a4a1cd7 | Books | $785.62 | 186 | ★ 4.5 | 70 | BULK-df87375c |
| 24654 | Bulk Product 79398654 | Clothing | $211.11 | 313 | ★ 1.4 | 4 | BULK-dc49790b |
| 24655 | Bulk Product da26453c | Clothing | $700.91 | 293 | ★ 1.3 | 94 | BULK-4b7dca4a |
| 24656 | Bulk Product bf9fb691 | Clothing | $84.13 | 209 | ★ 2.8 | 17 | BULK-bf04e4da |
| 24657 | Bulk Product 7168a7cc | Electronics | $189.77 | 161 | ★ 4.6 | 56 | BULK-682c3128 |
| 24658 | Bulk Product dadcb113 | Clothing | $431.02 | 284 | ★ 0.4 | 70 | BULK-925d08e0 |
| 24659 | Bulk Product 5cc37f5a | Sports & Outdoors | $940.97 | 15 | ★ 0.7 | 26 | BULK-c12e9321 |
// 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
};
}