Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 20785 | Bulk Product d38c1c6a | Books | $289.14 | 145 | ★ 0.9 | 4 | BULK-30e810c6 |
| 20786 | Bulk Product 7b318a37 | Home & Garden | $1,009.70 | 274 | ★ 0.4 | 6 | BULK-32b2a30f |
| 20787 | Bulk Product b77f992c | Electronics | $923.23 | 115 | ★ 4.6 | 25 | BULK-d2b7f44c |
| 20788 | Bulk Product f95c2fe3 | Books | $692.51 | 61 | ★ 2.5 | 7 | BULK-7c66d076 |
| 20789 | Bulk Product f1287316 | Home & Garden | $878.67 | 318 | ★ 0.4 | 41 | BULK-be419231 |
| 20790 | Bulk Product ac815d4a | Home & Garden | $289.82 | 107 | ★ 3.1 | 80 | BULK-7d2f2ee0 |
| 20791 | Bulk Product f6a01dbc | Books | $813.72 | 310 | ★ 4.2 | 49 | BULK-8f101405 |
| 20792 | Bulk Product 8e208b00 | Books | $42.45 | 374 | ★ 1.7 | 78 | BULK-3e0394e6 |
| 20793 | Bulk Product b45eac20 | Sports & Outdoors | $582.93 | 137 | ★ 1.6 | 54 | BULK-90db1bda |
| 20794 | Bulk Product 2fd55362 | Books | $530.04 | 4 | ★ 3.9 | 89 | BULK-0b768b5a |
| 20795 | Bulk Product c5c14194 | Home & Garden | $733.78 | 453 | ★ 4.1 | 47 | BULK-1d8c2916 |
| 20796 | Bulk Product 4c475cd5 | Sports & Outdoors | $777.26 | 414 | ★ 2.6 | 29 | BULK-c1bb4fc1 |
| 20797 | Bulk Product 0ab99fc1 | Electronics | $973.09 | 201 | ★ 1.2 | 92 | BULK-2fd17fb1 |
| 20798 | Bulk Product 64417f65 | Clothing | $337.18 | 362 | ★ 0.1 | 18 | BULK-467c95b9 |
| 20799 | Bulk Product 050f1f86 | Electronics | $938.64 | 258 | ★ 2.8 | 76 | BULK-2491a895 |
| 20800 | Bulk Product e2e3de79 | Toys & Games | $1,001.94 | 214 | ★ 3.5 | 34 | BULK-37308a9d |
| 20801 | Bulk Product b942ed88 | Sports & Outdoors | $740.62 | 396 | ★ 1.8 | 25 | BULK-1d9a6341 |
| 20802 | Bulk Product 424e650d | Toys & Games | $371.76 | 19 | ★ 3.4 | 96 | BULK-a2b95793 |
| 20803 | Bulk Product 1fe6e5e9 | Sports & Outdoors | $218.68 | 159 | ★ 1.8 | 57 | BULK-fbe12030 |
| 20804 | Bulk Product 8bb8c22d | Toys & Games | $124.80 | 109 | ★ 4.3 | 43 | BULK-295c18d4 |
| 20805 | Bulk Product ce6d0637 | Clothing | $134.72 | 83 | ★ 4.7 | 77 | BULK-b913bf41 |
| 20806 | Bulk Product 226ac0ae | Clothing | $626.92 | 113 | ★ 2.7 | 61 | BULK-a6cbca05 |
| 20807 | Bulk Product 8d5f9bb2 | Sports & Outdoors | $308.36 | 305 | ★ 2.6 | 23 | BULK-9a1a53b0 |
| 20808 | Bulk Product 4c5ef291 | Electronics | $971.91 | 483 | ★ 1.4 | 89 | BULK-7efd58eb |
| 20809 | Bulk Product 5e10ec87 | Electronics | $705.77 | 378 | ★ 3.0 | 89 | BULK-caa51402 |
// 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
};
}