Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 12185 | Bulk Product 85db85d7 | Electronics | $287.98 | 35 | ★ 1.5 | 20 | BULK-95877a15 |
| 12186 | Bulk Product 4c4f4895 | Home & Garden | $392.13 | 182 | ★ 1.5 | 38 | BULK-bc04adf5 |
| 12187 | Bulk Product fd67cdb0 | Clothing | $217.08 | 207 | ★ 0.6 | 56 | BULK-7acce21c |
| 12188 | Bulk Product 4dd2ef0c | Sports & Outdoors | $123.15 | 451 | ★ 4.4 | 1 | BULK-fa97c1ad |
| 12189 | Bulk Product 9a8f6920 | Books | $496.86 | 458 | ★ 1.7 | 39 | BULK-e2521944 |
| 12190 | Bulk Product c991a6ab | Clothing | $71.82 | 162 | ★ 0.9 | 13 | BULK-cf776948 |
| 12191 | Bulk Product 34662708 | Sports & Outdoors | $998.91 | 176 | ★ 3.9 | 3 | BULK-c9577b4d |
| 12192 | Bulk Product a1cd8692 | Clothing | $216.79 | 164 | ★ 0.2 | 26 | BULK-b44a44eb |
| 12193 | Bulk Product af37e899 | Sports & Outdoors | $642.13 | 307 | ★ 1.5 | 96 | BULK-f7e46e4b |
| 12194 | Bulk Product ac5be68b | Books | $548.30 | 39 | ★ 3.2 | 61 | BULK-05a84979 |
| 12195 | Bulk Product 36643d2e | Books | $722.12 | 109 | ★ 3.5 | 36 | BULK-bfdb0d9d |
| 12196 | Bulk Product f7d63cf9 | Home & Garden | $839.04 | 368 | ★ 2.7 | 4 | BULK-064218fb |
| 12197 | Bulk Product 98c7ed95 | Home & Garden | $864.64 | 114 | ★ 2.4 | 50 | BULK-59021396 |
| 12198 | Bulk Product 2f1dd2bc | Toys & Games | $411.38 | 95 | ★ 3.4 | 50 | BULK-9f27e84c |
| 12199 | Bulk Product 100a88af | Clothing | $803.23 | 405 | ★ 3.8 | 49 | BULK-8e663ea9 |
| 12200 | Bulk Product 690149f3 | Sports & Outdoors | $729.35 | 78 | ★ 0.4 | 28 | BULK-27d8f853 |
| 12201 | Bulk Product 645d73f1 | Toys & Games | $43.97 | 497 | ★ 1.4 | 42 | BULK-733ae973 |
| 12202 | Bulk Product fe1e52a7 | Clothing | $800.25 | 151 | ★ 1.2 | 86 | BULK-d1b53259 |
| 12203 | Bulk Product 732cd11a | Books | $847.97 | 437 | ★ 1.0 | 79 | BULK-eda3e3a2 |
| 12204 | Bulk Product 726ce91d | Electronics | $943.50 | 394 | ★ 1.0 | 34 | BULK-ae65f97c |
| 12205 | Bulk Product f761e05c | Electronics | $342.64 | 80 | ★ 3.0 | 43 | BULK-042585a4 |
| 12206 | Bulk Product 786234a2 | Sports & Outdoors | $416.63 | 434 | ★ 1.6 | 92 | BULK-b44b660a |
| 12207 | Bulk Product d4a243d8 | Clothing | $482.75 | 179 | ★ 1.5 | 7 | BULK-fd707cca |
| 12208 | Bulk Product b5e57b23 | Toys & Games | $853.92 | 60 | ★ 3.5 | 85 | BULK-13a59b50 |
| 12209 | Bulk Product 01c2c589 | Electronics | $769.91 | 151 | ★ 1.4 | 92 | BULK-ed9a742e |
// 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
};
}