Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 23460 | Bulk Product 0ecf0403 | Books | $144.57 | 474 | ★ 4.4 | 6 | BULK-6f16106c |
| 23461 | Bulk Product e2918429 | Home & Garden | $299.33 | 42 | ★ 0.8 | 36 | BULK-995527b7 |
| 23462 | Bulk Product 9bbda9c8 | Electronics | $307.26 | 474 | ★ 2.9 | 10 | BULK-c0730f0c |
| 23463 | Bulk Product f95af363 | Electronics | $707.18 | 338 | ★ 1.7 | 1 | BULK-7b2b5245 |
| 23464 | Bulk Product 1d0aa56f | Electronics | $796.02 | 85 | ★ 1.7 | 94 | BULK-d4c30a9a |
| 23465 | Bulk Product 7fbe7fed | Electronics | $805.21 | 222 | ★ 4.4 | 7 | BULK-104ec12c |
| 23466 | Bulk Product 7457b80a | Toys & Games | $795.00 | 316 | ★ 4.8 | 4 | BULK-30e0e86f |
| 23467 | Bulk Product 8811bd22 | Home & Garden | $970.96 | 158 | ★ 2.0 | 85 | BULK-17dddb03 |
| 23468 | Bulk Product 0502c190 | Books | $919.34 | 451 | ★ 4.8 | 64 | BULK-b69ce0a4 |
| 23469 | Bulk Product 66993926 | Electronics | $778.32 | 397 | ★ 2.8 | 65 | BULK-491e2625 |
| 23470 | Bulk Product e7ac7f5c | Home & Garden | $847.91 | 188 | ★ 2.6 | 77 | BULK-a0d8d824 |
| 23471 | Bulk Product 7f1f9163 | Home & Garden | $998.27 | 281 | ★ 4.5 | 28 | BULK-8b78400e |
| 23472 | Bulk Product ffcbbadd | Electronics | $1,003.57 | 469 | ★ 2.0 | 22 | BULK-7c914639 |
| 23473 | Bulk Product c93a7a1d | Sports & Outdoors | $726.49 | 392 | ★ 3.3 | 53 | BULK-411a1c60 |
| 23474 | Bulk Product 9362052e | Sports & Outdoors | $558.48 | 410 | ★ 2.3 | 50 | BULK-0822a046 |
| 23475 | Bulk Product 138935b1 | Home & Garden | $972.98 | 361 | ★ 2.7 | 10 | BULK-85732e27 |
| 23476 | Bulk Product a3f068ec | Books | $921.54 | 60 | ★ 1.9 | 6 | BULK-d79e30ca |
| 23477 | Bulk Product 61c5ec05 | Toys & Games | $344.81 | 321 | ★ 1.1 | 17 | BULK-9a8997cd |
| 23478 | Bulk Product 90af1e32 | Electronics | $551.96 | 78 | ★ 1.9 | 53 | BULK-4a69aea2 |
| 23479 | Bulk Product 86e0a759 | Sports & Outdoors | $948.70 | 468 | ★ 3.0 | 86 | BULK-c25833b0 |
| 23480 | Bulk Product 7cb42a82 | Clothing | $918.43 | 267 | ★ 4.4 | 55 | BULK-ec5d9084 |
| 23481 | Bulk Product ba124600 | Books | $714.49 | 165 | ★ 0.8 | 98 | BULK-08a4d378 |
| 23482 | Bulk Product 61ac9618 | Toys & Games | $727.39 | 309 | ★ 2.0 | 79 | BULK-17471369 |
| 23483 | Bulk Product bff5e391 | Electronics | $680.05 | 368 | ★ 3.6 | 64 | BULK-656a1ad8 |
| 23484 | Bulk Product 67958e51 | Clothing | $855.37 | 236 | ★ 1.0 | 23 | BULK-9cc56f62 |
// 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
};
}