Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 10260 | Bulk Product 34e46e82 | Home & Garden | $847.69 | 392 | ★ 3.1 | 99 | BULK-8d1b0e4f |
| 10261 | Bulk Product e44f766f | Books | $17.49 | 94 | ★ 3.3 | 5 | BULK-349225f3 |
| 10262 | Bulk Product 295e75c9 | Electronics | $392.12 | 265 | ★ 0.2 | 7 | BULK-90385cb3 |
| 10263 | Bulk Product 6cf2afba | Sports & Outdoors | $816.18 | 44 | ★ 1.4 | 76 | BULK-418f2d74 |
| 10264 | Bulk Product 4750b15f | Books | $339.32 | 166 | ★ 0.4 | 42 | BULK-ec3071a4 |
| 10265 | Bulk Product d6642c94 | Clothing | $427.70 | 393 | ★ 2.5 | 96 | BULK-cc7b4944 |
| 10266 | Bulk Product 0957a8af | Clothing | $265.08 | 60 | ★ 1.4 | 15 | BULK-20f35dc7 |
| 10267 | Bulk Product 6244be30 | Toys & Games | $55.70 | 320 | ★ 2.6 | 53 | BULK-901f29eb |
| 10268 | Bulk Product 98083f84 | Books | $14.03 | 258 | ★ 5.0 | 31 | BULK-195bd6fa |
| 10269 | Bulk Product 00f034ea | Electronics | $379.06 | 0 | ★ 3.7 | 40 | BULK-f6423851 |
| 10270 | Bulk Product 54410339 | Home & Garden | $563.55 | 183 | ★ 4.3 | 74 | BULK-5777e691 |
| 10271 | Bulk Product d503c213 | Electronics | $228.31 | 87 | ★ 5.0 | 95 | BULK-39754bc0 |
| 10272 | Bulk Product 8a87cb55 | Sports & Outdoors | $277.25 | 433 | ★ 4.1 | 22 | BULK-9b40dead |
| 10273 | Bulk Product 70677df4 | Clothing | $144.17 | 237 | ★ 1.0 | 44 | BULK-e79eac78 |
| 10274 | Bulk Product 87febb68 | Home & Garden | $720.22 | 240 | ★ 1.4 | 53 | BULK-21a78661 |
| 10275 | Bulk Product 262c2b8e | Toys & Games | $260.50 | 33 | ★ 3.3 | 12 | BULK-f53fc7e3 |
| 10276 | Bulk Product 856341b5 | Clothing | $80.51 | 354 | ★ 5.0 | 35 | BULK-f172bc31 |
| 10277 | Bulk Product 3e555e35 | Clothing | $109.58 | 279 | ★ 3.3 | 28 | BULK-b8633ac8 |
| 10278 | Bulk Product 80f22185 | Sports & Outdoors | $586.69 | 230 | ★ 3.0 | 53 | BULK-0c47cd72 |
| 10279 | Bulk Product 1057ba06 | Sports & Outdoors | $877.76 | 262 | ★ 1.9 | 63 | BULK-f64f2115 |
| 10280 | Bulk Product 82b55d3c | Books | $409.09 | 308 | ★ 0.2 | 81 | BULK-87be415c |
| 10281 | Bulk Product d4726faf | Home & Garden | $542.34 | 220 | ★ 1.5 | 63 | BULK-a81ac489 |
| 10282 | Bulk Product 3fa49e3d | Books | $745.81 | 393 | ★ 0.2 | 4 | BULK-87815505 |
| 10283 | Bulk Product 55dcabe1 | Electronics | $721.41 | 151 | ★ 1.3 | 86 | BULK-141fbebc |
| 10284 | Bulk Product 3bbb52b7 | Sports & Outdoors | $182.57 | 347 | ★ 1.5 | 49 | BULK-ca26af4b |
// 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
};
}