Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 19035 | Bulk Product 3c71cbc2 | Electronics | $609.68 | 330 | ★ 3.5 | 90 | BULK-084a5860 |
| 19036 | Bulk Product 63102a1a | Sports & Outdoors | $106.69 | 254 | ★ 3.6 | 84 | BULK-a5060711 |
| 19037 | Bulk Product 60ad0481 | Home & Garden | $851.18 | 198 | ★ 4.4 | 52 | BULK-a90acb8f |
| 19038 | Bulk Product c7b70a24 | Home & Garden | $975.38 | 355 | ★ 1.3 | 34 | BULK-8c480290 |
| 19039 | Bulk Product 69e160ac | Books | $280.17 | 20 | ★ 4.3 | 67 | BULK-4830e3e9 |
| 19040 | Bulk Product 7558f978 | Toys & Games | $169.46 | 126 | ★ 1.2 | 65 | BULK-6ec182ab |
| 19041 | Bulk Product 4a73dc6d | Books | $772.02 | 78 | ★ 1.0 | 7 | BULK-ae023a72 |
| 19042 | Bulk Product 95277465 | Home & Garden | $573.29 | 258 | ★ 0.5 | 49 | BULK-a793088a |
| 19043 | Bulk Product 68f4a6ea | Home & Garden | $910.74 | 458 | ★ 1.9 | 91 | BULK-238e0289 |
| 19044 | Bulk Product e8965768 | Toys & Games | $145.08 | 335 | ★ 1.4 | 28 | BULK-edba824c |
| 19045 | Bulk Product 005da923 | Sports & Outdoors | $501.69 | 253 | ★ 3.0 | 8 | BULK-ee377803 |
| 19046 | Bulk Product 21adfc6e | Home & Garden | $573.74 | 164 | ★ 0.8 | 64 | BULK-4b78f676 |
| 19047 | Bulk Product 02f253d1 | Sports & Outdoors | $317.22 | 484 | ★ 1.4 | 60 | BULK-31727e8d |
| 19048 | Bulk Product f7408892 | Electronics | $591.24 | 155 | ★ 4.0 | 53 | BULK-3a1700a9 |
| 19049 | Bulk Product a300d856 | Electronics | $160.28 | 332 | ★ 0.0 | 17 | BULK-411b3dbe |
| 19050 | Bulk Product 26b60a5c | Clothing | $104.80 | 125 | ★ 3.2 | 65 | BULK-835b0461 |
| 19051 | Bulk Product 1ad4c5c0 | Electronics | $697.71 | 188 | ★ 2.6 | 23 | BULK-6be85ed1 |
| 19052 | Bulk Product e04ffd1d | Sports & Outdoors | $480.27 | 317 | ★ 4.8 | 60 | BULK-17910573 |
| 19053 | Bulk Product 021cfe7b | Toys & Games | $625.77 | 464 | ★ 3.3 | 13 | BULK-5e48d81c |
| 19054 | Bulk Product 7e69eb3c | Electronics | $474.26 | 439 | ★ 2.9 | 99 | BULK-7ab43aa2 |
| 19055 | Bulk Product 73873b44 | Electronics | $897.47 | 74 | ★ 3.2 | 89 | BULK-8ca75e5e |
| 19056 | Bulk Product 784c0a66 | Books | $706.88 | 154 | ★ 0.0 | 10 | BULK-ffb46cfc |
| 19057 | Bulk Product 7d55bb0f | Toys & Games | $896.89 | 43 | ★ 1.8 | 85 | BULK-9ae1afbb |
| 19058 | Bulk Product e4fb9878 | Books | $703.61 | 330 | ★ 0.3 | 71 | BULK-dd0e5264 |
| 19059 | Bulk Product 3d3f0d6d | Toys & Games | $270.25 | 12 | ★ 4.2 | 4 | BULK-032280ab |
// 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
};
}