Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 12310 | Bulk Product ad45ac5b | Electronics | $892.24 | 271 | ★ 2.0 | 82 | BULK-7bff60c5 |
| 12311 | Bulk Product 5ebe4611 | Books | $923.88 | 175 | ★ 3.5 | 78 | BULK-13f68889 |
| 12312 | Bulk Product a30fdb43 | Toys & Games | $345.62 | 191 | ★ 0.5 | 88 | BULK-1b2e2f78 |
| 12313 | Bulk Product d20109fe | Toys & Games | $204.45 | 478 | ★ 3.1 | 64 | BULK-e7357e88 |
| 12314 | Bulk Product c8c2b679 | Sports & Outdoors | $672.96 | 490 | ★ 4.8 | 87 | BULK-90e97466 |
| 12315 | Bulk Product f33c0280 | Electronics | $857.63 | 418 | ★ 2.7 | 93 | BULK-e639bdc2 |
| 12316 | Bulk Product 614647c4 | Electronics | $128.81 | 436 | ★ 0.0 | 90 | BULK-4be65521 |
| 12317 | Bulk Product c7198def | Sports & Outdoors | $45.99 | 433 | ★ 0.1 | 19 | BULK-bd02153e |
| 12318 | Bulk Product 32d76516 | Books | $533.27 | 261 | ★ 4.4 | 79 | BULK-ef4c6fc5 |
| 12319 | Bulk Product 9a9cdd5e | Sports & Outdoors | $284.33 | 169 | ★ 4.9 | 13 | BULK-fe7f65ac |
| 12320 | Bulk Product 24bf52a3 | Sports & Outdoors | $936.22 | 254 | ★ 2.2 | 90 | BULK-495b3cc2 |
| 12321 | Bulk Product 74463f2f | Sports & Outdoors | $794.17 | 114 | ★ 4.9 | 52 | BULK-646c2acf |
| 12322 | Bulk Product 899e8d4c | Sports & Outdoors | $364.67 | 76 | ★ 2.0 | 58 | BULK-7ef5b02d |
| 12323 | Bulk Product 455697b0 | Home & Garden | $405.58 | 91 | ★ 1.4 | 59 | BULK-e638f2c6 |
| 12324 | Bulk Product 8670574b | Home & Garden | $933.35 | 241 | ★ 0.1 | 0 | BULK-4c7c5e8f |
| 12325 | Bulk Product 1845f258 | Electronics | $552.27 | 274 | ★ 3.0 | 81 | BULK-cf8b7aa2 |
| 12326 | Bulk Product 90d0ecf9 | Clothing | $258.86 | 149 | ★ 4.5 | 79 | BULK-6dd9ae13 |
| 12327 | Bulk Product 21d7136d | Electronics | $390.94 | 460 | ★ 3.3 | 37 | BULK-85cdaf63 |
| 12328 | Bulk Product 820aa625 | Sports & Outdoors | $461.66 | 226 | ★ 2.9 | 72 | BULK-c5ac5be8 |
| 12329 | Bulk Product a6337060 | Toys & Games | $359.91 | 86 | ★ 0.6 | 59 | BULK-5475a55a |
| 12330 | Bulk Product 7e3e6895 | Electronics | $336.81 | 395 | ★ 4.2 | 77 | BULK-e64858aa |
| 12331 | Bulk Product fa9d7dae | Clothing | $839.05 | 115 | ★ 1.1 | 76 | BULK-cb707b3e |
| 12332 | Bulk Product a7171e2f | Clothing | $601.53 | 178 | ★ 4.8 | 17 | BULK-82525f13 |
| 12333 | Bulk Product 01f2bb68 | Books | $237.11 | 61 | ★ 4.1 | 49 | BULK-e719b4a9 |
| 12334 | Bulk Product 9e5fcb18 | Home & Garden | $859.00 | 196 | ★ 4.6 | 8 | BULK-22974801 |
// 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
};
}