Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 24060 | Bulk Product e7246a55 | Home & Garden | $206.76 | 444 | ★ 1.9 | 12 | BULK-07060d18 |
| 24061 | Bulk Product 9f73412c | Sports & Outdoors | $323.67 | 336 | ★ 0.4 | 43 | BULK-b9ac2188 |
| 24062 | Bulk Product f4b23249 | Electronics | $573.25 | 254 | ★ 3.3 | 82 | BULK-c6702482 |
| 24063 | Bulk Product 6f879f15 | Clothing | $952.82 | 304 | ★ 1.2 | 39 | BULK-2b8696d3 |
| 24064 | Bulk Product 7e68f1bb | Clothing | $566.81 | 329 | ★ 1.2 | 52 | BULK-302fbd04 |
| 24065 | Bulk Product e375bd59 | Clothing | $153.15 | 184 | ★ 3.2 | 37 | BULK-cdab7bd2 |
| 24066 | Bulk Product 1f4d0a47 | Home & Garden | $96.17 | 85 | ★ 1.5 | 23 | BULK-35d480b4 |
| 24067 | Bulk Product f67dc436 | Electronics | $752.97 | 98 | ★ 2.6 | 95 | BULK-d61c4dfa |
| 24068 | Bulk Product eeafc796 | Books | $366.40 | 472 | ★ 0.7 | 96 | BULK-2464493a |
| 24069 | Bulk Product 301c43bc | Toys & Games | $13.55 | 105 | ★ 4.8 | 74 | BULK-b965d4e5 |
| 24070 | Bulk Product 5dcd43ac | Books | $308.21 | 349 | ★ 1.7 | 38 | BULK-4247f437 |
| 24071 | Bulk Product f5188ae5 | Electronics | $670.12 | 192 | ★ 1.9 | 39 | BULK-02ef1bea |
| 24072 | Bulk Product af1d8911 | Electronics | $252.06 | 99 | ★ 1.6 | 28 | BULK-51eaa7e8 |
| 24073 | Bulk Product aa22262d | Books | $657.68 | 97 | ★ 3.9 | 37 | BULK-a6d9ba15 |
| 24074 | Bulk Product 60758540 | Home & Garden | $812.69 | 462 | ★ 1.9 | 54 | BULK-8268893e |
| 24075 | Bulk Product 97d0c091 | Books | $567.25 | 202 | ★ 3.2 | 91 | BULK-4c7fc02e |
| 24076 | Bulk Product 83fac95e | Home & Garden | $652.07 | 416 | ★ 2.5 | 52 | BULK-b54c16af |
| 24077 | Bulk Product bdf56a8f | Books | $897.55 | 438 | ★ 4.1 | 3 | BULK-b45649cc |
| 24078 | Bulk Product 2bd9d1f3 | Sports & Outdoors | $685.58 | 387 | ★ 1.0 | 29 | BULK-a2aae873 |
| 24079 | Bulk Product ec8f4a73 | Clothing | $1,002.01 | 40 | ★ 1.6 | 47 | BULK-6e436951 |
| 24080 | Bulk Product dd2b59fc | Sports & Outdoors | $626.90 | 463 | ★ 0.1 | 17 | BULK-c59a0412 |
| 24081 | Bulk Product 164fb7bc | Home & Garden | $337.85 | 267 | ★ 0.4 | 93 | BULK-3277cb1b |
| 24082 | Bulk Product c53a867e | Books | $287.30 | 168 | ★ 1.9 | 68 | BULK-1dc73408 |
| 24083 | Bulk Product 48a16c5e | Clothing | $878.89 | 498 | ★ 3.9 | 78 | BULK-5ab9c4ed |
| 24084 | Bulk Product d7456b1f | Clothing | $440.23 | 287 | ★ 1.4 | 1 | BULK-e25bf459 |
// 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
};
}