Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 23310 | Bulk Product 8a48c751 | Electronics | $478.05 | 259 | ★ 4.3 | 38 | BULK-e4f08405 |
| 23311 | Bulk Product c65da7fd | Sports & Outdoors | $537.11 | 496 | ★ 1.8 | 79 | BULK-f63d7cd5 |
| 23312 | Bulk Product 20631a9b | Sports & Outdoors | $391.75 | 145 | ★ 2.5 | 89 | BULK-b7bf0cbd |
| 23313 | Bulk Product 1da280f5 | Home & Garden | $345.88 | 81 | ★ 0.6 | 84 | BULK-65833f25 |
| 23314 | Bulk Product b832dec6 | Home & Garden | $644.76 | 325 | ★ 3.0 | 26 | BULK-b845ffbe |
| 23315 | Bulk Product 93507f51 | Clothing | $282.60 | 19 | ★ 0.2 | 78 | BULK-f47200bb |
| 23316 | Bulk Product 9c0d84b3 | Clothing | $586.71 | 188 | ★ 0.1 | 10 | BULK-a69f937e |
| 23317 | Bulk Product 8571b341 | Electronics | $21.84 | 231 | ★ 4.7 | 91 | BULK-c7a39c81 |
| 23318 | Bulk Product add3aeaa | Clothing | $422.52 | 420 | ★ 2.3 | 86 | BULK-8d68252a |
| 23319 | Bulk Product 844f49c6 | Books | $320.22 | 439 | ★ 2.0 | 67 | BULK-3ebb305a |
| 23320 | Bulk Product 6d3a5a90 | Toys & Games | $881.93 | 168 | ★ 1.7 | 88 | BULK-7a86fef3 |
| 23321 | Bulk Product 522341ca | Clothing | $111.31 | 415 | ★ 2.6 | 23 | BULK-3754fe76 |
| 23322 | Bulk Product 092a4eb9 | Books | $951.36 | 200 | ★ 2.9 | 48 | BULK-4c556c51 |
| 23323 | Bulk Product 81a26a05 | Books | $384.06 | 492 | ★ 0.7 | 32 | BULK-c60b30ee |
| 23324 | Bulk Product 59273ca6 | Books | $379.14 | 283 | ★ 3.7 | 7 | BULK-f8102a00 |
| 23325 | Bulk Product 526e9791 | Electronics | $45.19 | 473 | ★ 4.5 | 95 | BULK-a316cbcf |
| 23326 | Bulk Product afd68e6a | Clothing | $673.29 | 488 | ★ 3.5 | 77 | BULK-72c1af03 |
| 23327 | Bulk Product 2f92afc3 | Sports & Outdoors | $938.42 | 474 | ★ 1.8 | 64 | BULK-b09200e9 |
| 23328 | Bulk Product c517a597 | Sports & Outdoors | $93.48 | 384 | ★ 4.6 | 98 | BULK-744dd4b5 |
| 23329 | Bulk Product 97a602ae | Clothing | $777.14 | 342 | ★ 1.4 | 5 | BULK-134825be |
| 23330 | Bulk Product 0474ae0b | Sports & Outdoors | $18.93 | 281 | ★ 0.7 | 60 | BULK-8e3bb78c |
| 23331 | Bulk Product 773307f5 | Home & Garden | $468.92 | 274 | ★ 1.0 | 89 | BULK-6dfd2b30 |
| 23332 | Bulk Product c90a4153 | Clothing | $90.26 | 117 | ★ 0.3 | 81 | BULK-7cf34ccf |
| 23333 | Bulk Product a9544990 | Home & Garden | $812.12 | 49 | ★ 3.0 | 29 | BULK-1301f9a4 |
| 23334 | Bulk Product fd47dd5c | Books | $809.88 | 418 | ★ 3.2 | 11 | BULK-9e6db5c2 |
// 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
};
}