Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 15910 | Bulk Product 954b1947 | Electronics | $880.12 | 178 | ★ 2.0 | 41 | BULK-ab81ac66 |
| 15911 | Bulk Product 39be2db0 | Home & Garden | $214.93 | 299 | ★ 2.0 | 5 | BULK-786ba089 |
| 15912 | Bulk Product 5f54ddf4 | Sports & Outdoors | $529.74 | 308 | ★ 3.3 | 25 | BULK-c9aae1b8 |
| 15913 | Bulk Product 8c263380 | Sports & Outdoors | $22.52 | 283 | ★ 0.5 | 73 | BULK-c0a76a54 |
| 15914 | Bulk Product d4abe767 | Books | $58.53 | 188 | ★ 1.6 | 37 | BULK-2720377d |
| 15915 | Bulk Product e063e75b | Home & Garden | $550.67 | 201 | ★ 0.0 | 45 | BULK-5e16c70b |
| 15916 | Bulk Product fc5de1bf | Sports & Outdoors | $585.28 | 93 | ★ 1.9 | 31 | BULK-e4117691 |
| 15917 | Bulk Product a326eeb9 | Toys & Games | $540.83 | 279 | ★ 5.0 | 31 | BULK-bae6cd42 |
| 15918 | Bulk Product 598ef19d | Toys & Games | $829.90 | 274 | ★ 3.2 | 6 | BULK-c9e5bfb7 |
| 15919 | Bulk Product a63ea570 | Clothing | $291.72 | 311 | ★ 2.4 | 56 | BULK-4facdb26 |
| 15920 | Bulk Product 0a103b94 | Toys & Games | $161.22 | 406 | ★ 0.2 | 16 | BULK-bf6ef79f |
| 15921 | Bulk Product fffdb052 | Clothing | $438.14 | 63 | ★ 1.6 | 35 | BULK-9f2500f2 |
| 15922 | Bulk Product f59cc4f4 | Electronics | $173.66 | 431 | ★ 2.2 | 96 | BULK-6aa3ff51 |
| 15923 | Bulk Product d7902c24 | Clothing | $110.37 | 401 | ★ 2.5 | 87 | BULK-9b4c559e |
| 15924 | Bulk Product 77adf6b2 | Electronics | $356.48 | 482 | ★ 1.8 | 94 | BULK-688eba96 |
| 15925 | Bulk Product 3b77d25f | Toys & Games | $838.14 | 308 | ★ 2.6 | 45 | BULK-13d72e3c |
| 15926 | Bulk Product 67f61769 | Home & Garden | $193.11 | 45 | ★ 2.0 | 92 | BULK-b21cec6c |
| 15927 | Bulk Product 5416b485 | Books | $82.29 | 142 | ★ 3.8 | 13 | BULK-165d8136 |
| 15928 | Bulk Product d9ce69c8 | Toys & Games | $691.65 | 67 | ★ 3.5 | 26 | BULK-023c1270 |
| 15929 | Bulk Product 91ce1a93 | Books | $849.88 | 352 | ★ 2.9 | 32 | BULK-3a753662 |
| 15930 | Bulk Product 7960e4ad | Sports & Outdoors | $292.55 | 443 | ★ 4.7 | 25 | BULK-b547ac66 |
| 15931 | Bulk Product 0b5672fa | Books | $798.57 | 260 | ★ 1.8 | 45 | BULK-fcba2a52 |
| 15932 | Bulk Product 9c0f0686 | Sports & Outdoors | $847.57 | 3 | ★ 1.3 | 48 | BULK-78ffdbbc |
| 15933 | Bulk Product 4173ecb5 | Home & Garden | $168.45 | 324 | ★ 4.1 | 93 | BULK-d54d17e2 |
| 15934 | Bulk Product 0a3930b7 | Books | $195.93 | 16 | ★ 4.5 | 4 | BULK-67fa3b54 |
// 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
};
}