Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 20085 | Bulk Product b2c1ffd3 | Books | $837.14 | 144 | ★ 4.0 | 67 | BULK-ce62c0d5 |
| 20086 | Bulk Product e9f66897 | Toys & Games | $600.12 | 236 | ★ 0.1 | 67 | BULK-47c6a591 |
| 20087 | Bulk Product 9b4a08c6 | Electronics | $274.11 | 131 | ★ 0.7 | 66 | BULK-6af234ad |
| 20088 | Bulk Product d0ff5ba6 | Sports & Outdoors | $780.36 | 237 | ★ 1.5 | 71 | BULK-09d0d05a |
| 20089 | Bulk Product bfab089a | Clothing | $796.33 | 62 | ★ 0.8 | 7 | BULK-e08144eb |
| 20090 | Bulk Product 3af14c6d | Clothing | $366.56 | 295 | ★ 1.0 | 22 | BULK-640c67e4 |
| 20091 | Bulk Product 38268084 | Books | $741.71 | 245 | ★ 2.4 | 33 | BULK-5539ba7f |
| 20092 | Bulk Product 3e5f5281 | Sports & Outdoors | $460.07 | 73 | ★ 4.1 | 54 | BULK-547a5711 |
| 20093 | Bulk Product 5120163c | Electronics | $838.17 | 48 | ★ 0.3 | 93 | BULK-d0b99a89 |
| 20094 | Bulk Product 513d8f50 | Sports & Outdoors | $194.66 | 488 | ★ 1.7 | 92 | BULK-43ab30d0 |
| 20095 | Bulk Product c265916a | Toys & Games | $34.29 | 242 | ★ 2.6 | 76 | BULK-8d26781d |
| 20096 | Bulk Product 562d0a42 | Toys & Games | $573.41 | 421 | ★ 3.7 | 18 | BULK-26210d83 |
| 20097 | Bulk Product 9f7797b7 | Clothing | $535.70 | 274 | ★ 0.6 | 13 | BULK-c061b757 |
| 20098 | Bulk Product 360cace9 | Toys & Games | $752.59 | 53 | ★ 4.6 | 6 | BULK-7dadecaa |
| 20099 | Bulk Product b13de8be | Clothing | $288.18 | 291 | ★ 0.1 | 91 | BULK-3e1a7db2 |
| 20100 | Bulk Product ec2acd8e | Sports & Outdoors | $660.64 | 86 | ★ 4.4 | 33 | BULK-1961785e |
| 20101 | Bulk Product 67ccf610 | Electronics | $334.04 | 73 | ★ 1.9 | 16 | BULK-a058e594 |
| 20102 | Bulk Product 06bd730d | Home & Garden | $647.46 | 353 | ★ 2.7 | 52 | BULK-6e4d92be |
| 20103 | Bulk Product 14761046 | Home & Garden | $299.16 | 245 | ★ 3.4 | 23 | BULK-7e72a21d |
| 20104 | Bulk Product 82a64aff | Electronics | $465.77 | 320 | ★ 0.5 | 52 | BULK-402cb6fc |
| 20105 | Bulk Product b5439670 | Books | $281.06 | 269 | ★ 3.4 | 32 | BULK-86ecbd15 |
| 20106 | Bulk Product 7c9e1c55 | Sports & Outdoors | $328.44 | 210 | ★ 5.0 | 42 | BULK-908eb543 |
| 20107 | Bulk Product 37ac7a0e | Sports & Outdoors | $508.21 | 35 | ★ 3.7 | 70 | BULK-82974856 |
| 20108 | Bulk Product ab0c962a | Electronics | $181.58 | 461 | ★ 1.9 | 67 | BULK-7028e1d5 |
| 20109 | Bulk Product 9bd6228d | Books | $784.12 | 286 | ★ 3.9 | 10 | BULK-b2457e31 |
// 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
};
}