Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 17185 | Bulk Product 6d62dd12 | Sports & Outdoors | $576.30 | 113 | ★ 0.5 | 39 | BULK-0091c082 |
| 17186 | Bulk Product f2b2096a | Clothing | $216.55 | 303 | ★ 4.0 | 86 | BULK-60f2030a |
| 17187 | Bulk Product b3471823 | Toys & Games | $188.19 | 429 | ★ 1.5 | 96 | BULK-9e0cb85e |
| 17188 | Bulk Product 9d505832 | Home & Garden | $658.06 | 6 | ★ 2.1 | 49 | BULK-1d954b13 |
| 17189 | Bulk Product 211f50cf | Sports & Outdoors | $187.36 | 158 | ★ 1.6 | 79 | BULK-8d294f05 |
| 17190 | Bulk Product 0d67998f | Toys & Games | $958.89 | 5 | ★ 0.2 | 2 | BULK-8b01657b |
| 17191 | Bulk Product 7b89ee55 | Toys & Games | $773.53 | 94 | ★ 1.6 | 38 | BULK-9ced82bc |
| 17192 | Bulk Product 98b8d216 | Books | $274.52 | 340 | ★ 2.1 | 50 | BULK-4828c0de |
| 17193 | Bulk Product b33d3408 | Home & Garden | $883.72 | 397 | ★ 4.6 | 76 | BULK-5f8c0a96 |
| 17194 | Bulk Product 7ff6f212 | Home & Garden | $566.54 | 121 | ★ 5.0 | 44 | BULK-dd1e866f |
| 17195 | Bulk Product 29f16a2c | Toys & Games | $333.85 | 446 | ★ 0.9 | 79 | BULK-ed36daec |
| 17196 | Bulk Product 7bf60a1f | Books | $749.33 | 269 | ★ 5.0 | 20 | BULK-5aa57607 |
| 17197 | Bulk Product 1d91492a | Books | $500.38 | 429 | ★ 1.5 | 70 | BULK-b0ec254f |
| 17198 | Bulk Product ace8cb75 | Electronics | $226.41 | 176 | ★ 3.2 | 60 | BULK-d9490f52 |
| 17199 | Bulk Product 1a396827 | Home & Garden | $278.47 | 406 | ★ 2.2 | 9 | BULK-7002cc6c |
| 17200 | Bulk Product 638fe940 | Clothing | $669.50 | 471 | ★ 2.2 | 28 | BULK-d7edf575 |
| 17201 | Bulk Product 9509ab0c | Electronics | $820.99 | 470 | ★ 4.0 | 72 | BULK-3a837084 |
| 17202 | Bulk Product 4bc33ff1 | Sports & Outdoors | $755.51 | 118 | ★ 4.4 | 80 | BULK-85013621 |
| 17203 | Bulk Product b1fcc803 | Electronics | $141.59 | 106 | ★ 2.4 | 81 | BULK-ebc9d6eb |
| 17204 | Bulk Product f36f3d7f | Home & Garden | $297.61 | 357 | ★ 0.1 | 0 | BULK-24320d60 |
| 17205 | Bulk Product 0eeda127 | Home & Garden | $878.46 | 103 | ★ 3.1 | 24 | BULK-97613ee2 |
| 17206 | Bulk Product 15249441 | Sports & Outdoors | $350.05 | 368 | ★ 1.4 | 11 | BULK-53caaa4b |
| 17207 | Bulk Product f1186051 | Books | $829.15 | 57 | ★ 1.1 | 36 | BULK-91bb6154 |
| 17208 | Bulk Product 13291fa2 | Toys & Games | $138.40 | 46 | ★ 2.0 | 48 | BULK-a6b220d4 |
| 17209 | Bulk Product 5e3beae1 | Electronics | $51.19 | 263 | ★ 1.9 | 23 | BULK-594ac60a |
// 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
};
}