Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 15010 | Bulk Product f6dbf412 | Sports & Outdoors | $81.72 | 387 | ★ 1.7 | 35 | BULK-043939c0 |
| 15011 | Bulk Product 61450cdd | Books | $730.77 | 466 | ★ 2.0 | 67 | BULK-aa896f49 |
| 15012 | Bulk Product 0d1a37dc | Sports & Outdoors | $483.93 | 415 | ★ 0.8 | 81 | BULK-cc8710b2 |
| 15013 | Bulk Product 4ef76c4e | Toys & Games | $797.47 | 320 | ★ 1.6 | 6 | BULK-065aa678 |
| 15014 | Bulk Product 454bdf29 | Electronics | $581.60 | 304 | ★ 0.6 | 57 | BULK-07b5fcb1 |
| 15015 | Bulk Product 270f792a | Toys & Games | $794.14 | 95 | ★ 3.5 | 49 | BULK-06893fe8 |
| 15016 | Bulk Product bb8b818e | Electronics | $393.08 | 205 | ★ 4.6 | 8 | BULK-d40b8e15 |
| 15017 | Bulk Product 73bac3b2 | Books | $444.61 | 50 | ★ 2.9 | 53 | BULK-44f59b8b |
| 15018 | Bulk Product 69d15a1c | Sports & Outdoors | $232.41 | 236 | ★ 0.6 | 37 | BULK-f01c83cc |
| 15019 | Bulk Product 0f693d3f | Books | $861.35 | 26 | ★ 4.6 | 51 | BULK-09226739 |
| 15020 | Bulk Product fa890357 | Sports & Outdoors | $16.18 | 90 | ★ 2.1 | 76 | BULK-ffb16bb1 |
| 15021 | Bulk Product 33661580 | Books | $335.27 | 409 | ★ 2.5 | 90 | BULK-d619116b |
| 15022 | Bulk Product fa1d16f4 | Home & Garden | $754.20 | 400 | ★ 1.6 | 58 | BULK-9101d38d |
| 15023 | Bulk Product 7b46c26d | Toys & Games | $335.63 | 145 | ★ 0.5 | 16 | BULK-70ed4965 |
| 15024 | Bulk Product 1fbee69d | Home & Garden | $784.98 | 295 | ★ 0.6 | 37 | BULK-fef768db |
| 15025 | Bulk Product c23f26fc | Clothing | $666.13 | 147 | ★ 3.7 | 86 | BULK-9031f8a2 |
| 15026 | Bulk Product 1e7ff375 | Electronics | $910.88 | 42 | ★ 3.0 | 23 | BULK-d7416007 |
| 15027 | Bulk Product b65a9ac7 | Toys & Games | $82.11 | 206 | ★ 1.1 | 72 | BULK-d7e7713f |
| 15028 | Bulk Product 8290b457 | Toys & Games | $342.00 | 27 | ★ 4.9 | 44 | BULK-7ea9010f |
| 15029 | Bulk Product d25760ae | Electronics | $465.36 | 473 | ★ 1.8 | 33 | BULK-43c51d3a |
| 15030 | Bulk Product 1903b970 | Home & Garden | $156.56 | 475 | ★ 4.8 | 10 | BULK-c93f45e9 |
| 15031 | Bulk Product bdd116fe | Books | $166.83 | 344 | ★ 1.1 | 69 | BULK-d0066dc6 |
| 15032 | Bulk Product 051c2767 | Home & Garden | $716.31 | 482 | ★ 1.8 | 21 | BULK-9015125d |
| 15033 | Bulk Product a84838f5 | Books | $130.98 | 431 | ★ 2.2 | 91 | BULK-6a5d7094 |
| 15034 | Bulk Product 84142ad9 | Clothing | $902.06 | 490 | ★ 1.7 | 91 | BULK-11c75ebd |
// 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
};
}