Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 10135 | Bulk Product 0f472b7a | Books | $540.32 | 136 | ★ 4.3 | 16 | BULK-ca63cfc8 |
| 10136 | Bulk Product f1ca0325 | Home & Garden | $845.06 | 58 | ★ 3.8 | 33 | BULK-c50a9d67 |
| 10137 | Bulk Product bef9fae4 | Sports & Outdoors | $166.15 | 336 | ★ 2.7 | 69 | BULK-2963e67b |
| 10138 | Bulk Product e49db765 | Clothing | $687.73 | 236 | ★ 2.2 | 95 | BULK-58ec3f50 |
| 10139 | Bulk Product 458470d8 | Clothing | $246.40 | 455 | ★ 1.1 | 55 | BULK-11baa4bc |
| 10140 | Bulk Product 34d721c1 | Home & Garden | $962.74 | 333 | ★ 0.1 | 33 | BULK-dae7e5a3 |
| 10141 | Bulk Product f7001365 | Books | $454.15 | 155 | ★ 1.6 | 40 | BULK-6b768fdf |
| 10142 | Bulk Product be103cf2 | Books | $461.26 | 276 | ★ 3.0 | 73 | BULK-7e1ea0ed |
| 10143 | Bulk Product 8aae9762 | Electronics | $293.01 | 11 | ★ 1.9 | 85 | BULK-8b21465d |
| 10144 | Bulk Product 0d9f38b9 | Home & Garden | $423.45 | 491 | ★ 3.9 | 38 | BULK-3a126cbf |
| 10145 | Bulk Product eaaac38b | Home & Garden | $792.53 | 452 | ★ 4.1 | 8 | BULK-dff2b8a0 |
| 10146 | Bulk Product 8fe19c38 | Home & Garden | $664.44 | 133 | ★ 3.4 | 37 | BULK-9805def5 |
| 10147 | Bulk Product 25675931 | Clothing | $443.37 | 194 | ★ 0.1 | 17 | BULK-f9b5c6a9 |
| 10148 | Bulk Product cef5be6c | Home & Garden | $964.35 | 57 | ★ 2.0 | 70 | BULK-27f233af |
| 10149 | Bulk Product 0cea6bf0 | Home & Garden | $65.84 | 177 | ★ 2.7 | 78 | BULK-b388cd05 |
| 10150 | Bulk Product f3d4db2c | Electronics | $577.90 | 272 | ★ 3.0 | 48 | BULK-e74ae48d |
| 10151 | Bulk Product c02dee9c | Sports & Outdoors | $366.40 | 245 | ★ 0.4 | 12 | BULK-5ff85502 |
| 10152 | Bulk Product a9cff78b | Home & Garden | $426.16 | 104 | ★ 3.7 | 32 | BULK-28edf340 |
| 10153 | Bulk Product a26e183c | Electronics | $534.37 | 482 | ★ 1.8 | 94 | BULK-f82ee0a3 |
| 10154 | Bulk Product c7d6413b | Home & Garden | $264.03 | 351 | ★ 4.9 | 29 | BULK-28de3e9c |
| 10155 | Bulk Product 0e3d8e80 | Toys & Games | $519.66 | 93 | ★ 1.7 | 31 | BULK-295f65a5 |
| 10156 | Bulk Product edd4e3c8 | Books | $896.22 | 221 | ★ 4.5 | 60 | BULK-eda8e8ce |
| 10157 | Bulk Product e1200a28 | Clothing | $495.59 | 413 | ★ 3.9 | 3 | BULK-7992b581 |
| 10158 | Bulk Product 1ee8d1c5 | Toys & Games | $572.58 | 385 | ★ 2.6 | 49 | BULK-4b582d8a |
| 10159 | Bulk Product fd523235 | Clothing | $417.60 | 277 | ★ 0.1 | 32 | BULK-d3f36f2d |
// 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
};
}