Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 11835 | Bulk Product 2e62e74e | Electronics | $58.89 | 20 | ★ 2.8 | 42 | BULK-e64b6b55 |
| 11836 | Bulk Product 2dd6962e | Sports & Outdoors | $815.88 | 367 | ★ 3.8 | 43 | BULK-d40ad4d8 |
| 11837 | Bulk Product a0de75e9 | Clothing | $405.88 | 68 | ★ 1.7 | 64 | BULK-448b567a |
| 11838 | Bulk Product 3bbfbc1c | Toys & Games | $773.53 | 469 | ★ 3.2 | 82 | BULK-f6271868 |
| 11839 | Bulk Product 206a5516 | Clothing | $20.01 | 137 | ★ 1.8 | 75 | BULK-d1e18ec6 |
| 11840 | Bulk Product d04a06e1 | Books | $434.80 | 391 | ★ 3.2 | 46 | BULK-72a1888b |
| 11841 | Bulk Product f4e22404 | Clothing | $129.14 | 347 | ★ 4.8 | 71 | BULK-5ff6e8f1 |
| 11842 | Bulk Product 4226fc9d | Sports & Outdoors | $721.88 | 58 | ★ 1.4 | 28 | BULK-7ede690a |
| 11843 | Bulk Product 3b8a2fcc | Toys & Games | $647.57 | 407 | ★ 2.2 | 68 | BULK-d5e5b0fa |
| 11844 | Bulk Product 24deead5 | Toys & Games | $882.51 | 412 | ★ 2.3 | 95 | BULK-df9b1b82 |
| 11845 | Bulk Product 5c932ff8 | Toys & Games | $188.19 | 71 | ★ 3.8 | 55 | BULK-dcd9876c |
| 11846 | Bulk Product 4be53d09 | Sports & Outdoors | $970.75 | 465 | ★ 4.9 | 51 | BULK-b3a77a84 |
| 11847 | Bulk Product 54fad82e | Sports & Outdoors | $607.40 | 31 | ★ 3.9 | 38 | BULK-a7f9a428 |
| 11848 | Bulk Product a3c623df | Toys & Games | $38.50 | 51 | ★ 4.3 | 72 | BULK-73c7d878 |
| 11849 | Bulk Product 30542e42 | Clothing | $398.81 | 431 | ★ 0.5 | 32 | BULK-6adc32cd |
| 11850 | Bulk Product 8f4a086a | Clothing | $813.91 | 270 | ★ 1.5 | 40 | BULK-46bbe587 |
| 11851 | Bulk Product f0388dfe | Electronics | $388.92 | 99 | ★ 1.9 | 11 | BULK-e660570d |
| 11852 | Bulk Product db5bd664 | Toys & Games | $885.98 | 234 | ★ 2.1 | 71 | BULK-5456120a |
| 11853 | Bulk Product a0a4e124 | Books | $267.69 | 301 | ★ 2.6 | 48 | BULK-1f44fe01 |
| 11854 | Bulk Product eefc7162 | Electronics | $153.74 | 218 | ★ 0.9 | 52 | BULK-d3c21e41 |
| 11855 | Bulk Product 63094874 | Toys & Games | $288.35 | 304 | ★ 3.2 | 74 | BULK-11c8a3bb |
| 11856 | Bulk Product 3098223b | Toys & Games | $387.32 | 347 | ★ 3.3 | 13 | BULK-bbb19c19 |
| 11857 | Bulk Product 12852fca | Books | $15.82 | 273 | ★ 0.2 | 65 | BULK-fd9748d0 |
| 11858 | Bulk Product bb994d9e | Electronics | $810.10 | 355 | ★ 1.4 | 72 | BULK-1ab5f5d8 |
| 11859 | Bulk Product af0ace7a | Sports & Outdoors | $28.22 | 380 | ★ 1.1 | 40 | BULK-ce919e9a |
// 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
};
}