Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 19585 | Bulk Product b7140cae | Electronics | $733.74 | 274 | ★ 1.7 | 42 | BULK-20935f3c |
| 19586 | Bulk Product a282088e | Electronics | $906.00 | 414 | ★ 1.7 | 17 | BULK-5a9f6dca |
| 19587 | Bulk Product 67dcee50 | Books | $115.50 | 425 | ★ 3.1 | 86 | BULK-a889c9d1 |
| 19588 | Bulk Product d48b0d28 | Books | $489.89 | 396 | ★ 0.0 | 7 | BULK-b60cb519 |
| 19589 | Bulk Product 346147c7 | Toys & Games | $318.18 | 405 | ★ 3.6 | 17 | BULK-6de34bbc |
| 19590 | Bulk Product 00b71b33 | Electronics | $224.60 | 249 | ★ 4.3 | 94 | BULK-d4b469d9 |
| 19591 | Bulk Product 8c5927e0 | Electronics | $619.87 | 126 | ★ 2.6 | 30 | BULK-90430e20 |
| 19592 | Bulk Product e1fb7b3c | Home & Garden | $100.32 | 141 | ★ 4.1 | 42 | BULK-8058fce5 |
| 19593 | Bulk Product 95a5a728 | Sports & Outdoors | $340.58 | 378 | ★ 3.7 | 2 | BULK-1a4f35de |
| 19594 | Bulk Product 7ed5b09d | Sports & Outdoors | $778.70 | 375 | ★ 0.5 | 5 | BULK-104d65fa |
| 19595 | Bulk Product 38f62f87 | Sports & Outdoors | $577.39 | 175 | ★ 4.0 | 43 | BULK-b4080837 |
| 19596 | Bulk Product 2dd8ad70 | Clothing | $977.26 | 328 | ★ 2.7 | 55 | BULK-acc181c7 |
| 19597 | Bulk Product b3d79dc9 | Sports & Outdoors | $823.41 | 269 | ★ 3.9 | 18 | BULK-c451c383 |
| 19598 | Bulk Product 9aef3dc6 | Electronics | $427.76 | 10 | ★ 1.6 | 55 | BULK-18a2a34c |
| 19599 | Bulk Product e130a3fb | Books | $771.89 | 93 | ★ 3.8 | 61 | BULK-77d60263 |
| 19600 | Bulk Product 02131b0f | Electronics | $938.82 | 270 | ★ 3.3 | 65 | BULK-af1b8ec4 |
| 19601 | Bulk Product 94cd2105 | Sports & Outdoors | $638.35 | 50 | ★ 4.9 | 77 | BULK-f870c233 |
| 19602 | Bulk Product c66211cd | Books | $454.22 | 194 | ★ 3.4 | 89 | BULK-97876362 |
| 19603 | Bulk Product 17b6913e | Toys & Games | $951.62 | 318 | ★ 2.4 | 13 | BULK-bde770a7 |
| 19604 | Bulk Product 792e7796 | Home & Garden | $60.61 | 376 | ★ 0.8 | 88 | BULK-db60f225 |
| 19605 | Bulk Product 1097bcfc | Sports & Outdoors | $26.75 | 147 | ★ 1.2 | 12 | BULK-6a46afef |
| 19606 | Bulk Product 60b5ca75 | Sports & Outdoors | $28.76 | 452 | ★ 1.3 | 85 | BULK-cf67f9ae |
| 19607 | Bulk Product acd079ba | Electronics | $594.35 | 263 | ★ 1.1 | 7 | BULK-3518c07e |
| 19608 | Bulk Product ec940791 | Home & Garden | $858.82 | 23 | ★ 2.9 | 65 | BULK-8ef8e8fc |
| 19609 | Bulk Product 385e06ab | Home & Garden | $394.32 | 479 | ★ 1.5 | 1 | BULK-fcb50190 |
// 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
};
}