Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 18585 | Bulk Product 13853a38 | Sports & Outdoors | $228.29 | 335 | ★ 0.5 | 11 | BULK-c4a7a62d |
| 18586 | Bulk Product 9aac975a | Toys & Games | $196.54 | 373 | ★ 3.0 | 49 | BULK-0d97e82c |
| 18587 | Bulk Product 582d74b3 | Home & Garden | $490.42 | 426 | ★ 0.2 | 69 | BULK-0c27cefe |
| 18588 | Bulk Product 68645b96 | Clothing | $987.48 | 461 | ★ 4.5 | 41 | BULK-a7fd55a9 |
| 18589 | Bulk Product 0d48e45b | Clothing | $22.38 | 299 | ★ 3.4 | 59 | BULK-7b95be12 |
| 18590 | Bulk Product 3ed0b5d9 | Sports & Outdoors | $902.49 | 316 | ★ 0.6 | 24 | BULK-8e426d84 |
| 18591 | Bulk Product a7bdf98a | Electronics | $457.22 | 207 | ★ 4.1 | 71 | BULK-69d8b421 |
| 18592 | Bulk Product 529a827c | Home & Garden | $855.22 | 41 | ★ 4.1 | 57 | BULK-d55c3874 |
| 18593 | Bulk Product dd5dbc5b | Books | $939.95 | 378 | ★ 1.9 | 82 | BULK-3e70744e |
| 18594 | Bulk Product cde1ed9c | Clothing | $552.74 | 293 | ★ 0.9 | 95 | BULK-8710e649 |
| 18595 | Bulk Product 8027f7f3 | Sports & Outdoors | $412.11 | 413 | ★ 1.3 | 75 | BULK-1659e932 |
| 18596 | Bulk Product 03c4b151 | Clothing | $480.57 | 207 | ★ 1.4 | 26 | BULK-f1470f16 |
| 18597 | Bulk Product c4482a93 | Clothing | $352.50 | 354 | ★ 1.7 | 70 | BULK-0ec55919 |
| 18598 | Bulk Product 9fbc898b | Sports & Outdoors | $971.95 | 335 | ★ 2.8 | 82 | BULK-cc54b158 |
| 18599 | Bulk Product 3674edab | Home & Garden | $135.46 | 480 | ★ 5.0 | 1 | BULK-9f1f0e76 |
| 18600 | Bulk Product 9115ae68 | Home & Garden | $508.12 | 450 | ★ 1.0 | 78 | BULK-5b2ba22a |
| 18601 | Bulk Product 4c9101c3 | Home & Garden | $706.10 | 85 | ★ 0.8 | 0 | BULK-92e726e3 |
| 18602 | Bulk Product 28d04f26 | Electronics | $314.82 | 33 | ★ 4.4 | 0 | BULK-2051efd0 |
| 18603 | Bulk Product c7d176b0 | Home & Garden | $768.97 | 124 | ★ 2.4 | 69 | BULK-32293106 |
| 18604 | Bulk Product f69ccf44 | Sports & Outdoors | $164.69 | 185 | ★ 3.9 | 96 | BULK-baac7b7e |
| 18605 | Bulk Product b5fdecee | Electronics | $705.39 | 256 | ★ 3.4 | 70 | BULK-6bfe1d22 |
| 18606 | Bulk Product ba13113b | Clothing | $744.20 | 364 | ★ 1.6 | 22 | BULK-8d541936 |
| 18607 | Bulk Product 0f07cd99 | Electronics | $799.92 | 291 | ★ 5.0 | 77 | BULK-c192d1d5 |
| 18608 | Bulk Product ed97d789 | Home & Garden | $613.35 | 174 | ★ 0.0 | 98 | BULK-69649b4b |
| 18609 | Bulk Product 80df26ef | Electronics | $255.76 | 189 | ★ 2.9 | 65 | BULK-5b8296c8 |
// 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
};
}