Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 6985 | Bulk Product adccddde | Clothing | $141.56 | 109 | ★ 1.3 | 61 | BULK-38061eda |
| 6986 | Bulk Product 35f6b48d | Books | $912.18 | 498 | ★ 1.8 | 63 | BULK-94a4025c |
| 6987 | Bulk Product 5617d966 | Books | $557.29 | 138 | ★ 1.7 | 16 | BULK-4e681a75 |
| 6988 | Bulk Product 73646ace | Sports & Outdoors | $974.24 | 463 | ★ 0.9 | 57 | BULK-1dc316d8 |
| 6989 | Bulk Product 018f69b5 | Electronics | $886.26 | 474 | ★ 0.9 | 9 | BULK-68e68207 |
| 6990 | Bulk Product 443671f4 | Home & Garden | $888.20 | 240 | ★ 0.1 | 6 | BULK-17d8f596 |
| 6991 | Bulk Product aec8180b | Home & Garden | $581.21 | 291 | ★ 2.1 | 68 | BULK-74ff9708 |
| 6992 | Bulk Product 7d03c0ae | Sports & Outdoors | $683.58 | 209 | ★ 1.9 | 43 | BULK-95167cc0 |
| 6993 | Bulk Product 3027109d | Sports & Outdoors | $717.38 | 125 | ★ 1.3 | 7 | BULK-e6946d0e |
| 6994 | Bulk Product 0da72844 | Books | $500.89 | 220 | ★ 2.3 | 79 | BULK-d1cecada |
| 6995 | Bulk Product 69f9a536 | Clothing | $24.90 | 241 | ★ 3.8 | 75 | BULK-d0375cf6 |
| 6996 | Bulk Product 6ddfc2e3 | Toys & Games | $823.56 | 86 | ★ 2.9 | 84 | BULK-196b67c5 |
| 6997 | Bulk Product 27ebcb4b | Clothing | $275.62 | 59 | ★ 3.9 | 9 | BULK-c34532e1 |
| 6998 | Bulk Product 3d902442 | Sports & Outdoors | $955.33 | 209 | ★ 4.4 | 74 | BULK-28d41cd9 |
| 6999 | Bulk Product 76f84f52 | Electronics | $489.39 | 329 | ★ 2.3 | 37 | BULK-1902f5ff |
| 7000 | Bulk Product 7c62de1a | Electronics | $490.13 | 267 | ★ 3.6 | 75 | BULK-fcf77914 |
| 7001 | Bulk Product a9099534 | Home & Garden | $523.30 | 27 | ★ 3.6 | 59 | BULK-c7bc4d89 |
| 7002 | Bulk Product c5c63461 | Books | $588.05 | 293 | ★ 3.3 | 22 | BULK-20df830b |
| 7003 | Bulk Product 31353e51 | Home & Garden | $552.47 | 238 | ★ 3.4 | 1 | BULK-05473530 |
| 7004 | Bulk Product 70f1d99f | Toys & Games | $588.54 | 357 | ★ 2.8 | 9 | BULK-43a70cdf |
| 7005 | Bulk Product 966360c7 | Books | $223.14 | 128 | ★ 3.7 | 82 | BULK-c07093f0 |
| 7006 | Bulk Product deee57e5 | Toys & Games | $623.69 | 225 | ★ 0.0 | 75 | BULK-2abb9994 |
| 7007 | Bulk Product c7348914 | Sports & Outdoors | $233.81 | 176 | ★ 3.7 | 8 | BULK-25c183de |
| 7008 | Bulk Product 590e4845 | Books | $1,006.74 | 360 | ★ 1.1 | 84 | BULK-b7dea135 |
| 7009 | Bulk Product 06f5924b | Toys & Games | $277.29 | 447 | ★ 3.7 | 72 | BULK-1555c7aa |
// 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
};
}