Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 15835 | Bulk Product 3f4112f1 | Sports & Outdoors | $192.62 | 464 | ★ 2.0 | 31 | BULK-cf691dba |
| 15836 | Bulk Product 2cc77349 | Home & Garden | $822.74 | 441 | ★ 1.8 | 18 | BULK-3bc00614 |
| 15837 | Bulk Product 4eefe7f0 | Toys & Games | $574.09 | 369 | ★ 1.6 | 32 | BULK-ee847291 |
| 15838 | Bulk Product 2b99026c | Electronics | $539.14 | 254 | ★ 3.0 | 18 | BULK-1f084755 |
| 15839 | Bulk Product 2cc16fb4 | Sports & Outdoors | $889.88 | 36 | ★ 4.2 | 52 | BULK-6fa89fd9 |
| 15840 | Bulk Product e143f079 | Books | $554.86 | 253 | ★ 2.0 | 64 | BULK-d0ff2e34 |
| 15841 | Bulk Product 940d752d | Clothing | $606.82 | 246 | ★ 2.8 | 74 | BULK-722b6281 |
| 15842 | Bulk Product bbb10787 | Electronics | $497.64 | 499 | ★ 2.5 | 19 | BULK-46348976 |
| 15843 | Bulk Product 46085497 | Home & Garden | $773.12 | 102 | ★ 3.8 | 99 | BULK-06790705 |
| 15844 | Bulk Product 4c5214fd | Sports & Outdoors | $219.55 | 72 | ★ 2.8 | 43 | BULK-9b938c77 |
| 15845 | Bulk Product e3e83588 | Toys & Games | $473.81 | 61 | ★ 1.5 | 38 | BULK-dcbf7c25 |
| 15846 | Bulk Product c95aff9f | Electronics | $116.37 | 150 | ★ 4.0 | 50 | BULK-d09eccdc |
| 15847 | Bulk Product d776378d | Electronics | $464.27 | 437 | ★ 2.9 | 17 | BULK-fb56af8f |
| 15848 | Bulk Product bd986b8d | Home & Garden | $824.50 | 62 | ★ 4.2 | 59 | BULK-5b8278ff |
| 15849 | Bulk Product f51513c2 | Sports & Outdoors | $317.34 | 245 | ★ 3.2 | 3 | BULK-709cde40 |
| 15850 | Bulk Product 182e3abd | Toys & Games | $831.51 | 214 | ★ 3.1 | 40 | BULK-2ec938ca |
| 15851 | Bulk Product 1184ab32 | Books | $126.54 | 206 | ★ 3.2 | 48 | BULK-cfcefca6 |
| 15852 | Bulk Product a78f8702 | Books | $269.46 | 297 | ★ 2.0 | 23 | BULK-a7f07cfb |
| 15853 | Bulk Product 2ce21a01 | Home & Garden | $494.56 | 9 | ★ 1.8 | 98 | BULK-e6d8bdfd |
| 15854 | Bulk Product 8d59562b | Books | $851.87 | 444 | ★ 2.9 | 68 | BULK-1349664e |
| 15855 | Bulk Product fdeb3d2d | Electronics | $442.80 | 302 | ★ 2.0 | 70 | BULK-ec2b9e9c |
| 15856 | Bulk Product 58d8991b | Electronics | $505.02 | 411 | ★ 4.7 | 48 | BULK-1dba12ce |
| 15857 | Bulk Product 6d9c653a | Toys & Games | $981.02 | 35 | ★ 3.2 | 84 | BULK-30f9a5f6 |
| 15858 | Bulk Product 0b3e301f | Electronics | $855.32 | 423 | ★ 2.1 | 38 | BULK-653f8885 |
| 15859 | Bulk Product 1511031d | Electronics | $409.16 | 269 | ★ 2.2 | 45 | BULK-11f3dbcf |
// 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
};
}