Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 15210 | Bulk Product f1bd847b | Toys & Games | $448.60 | 346 | ★ 0.2 | 3 | BULK-4a89a3b6 |
| 15211 | Bulk Product 8195b366 | Toys & Games | $182.60 | 341 | ★ 1.2 | 30 | BULK-536f63e3 |
| 15212 | Bulk Product 3221871f | Electronics | $107.41 | 277 | ★ 4.7 | 36 | BULK-07ebc2db |
| 15213 | Bulk Product 00b3909e | Home & Garden | $897.66 | 316 | ★ 2.1 | 5 | BULK-49efd8bc |
| 15214 | Bulk Product 3c458bbf | Home & Garden | $956.62 | 165 | ★ 3.6 | 71 | BULK-7a7a2949 |
| 15215 | Bulk Product 762aa9d3 | Home & Garden | $897.59 | 225 | ★ 0.7 | 50 | BULK-bc627f4f |
| 15216 | Bulk Product 47bbf8de | Sports & Outdoors | $397.47 | 290 | ★ 3.9 | 54 | BULK-29c1ee61 |
| 15217 | Bulk Product 3808980e | Electronics | $569.01 | 5 | ★ 3.3 | 90 | BULK-ec4f0f8e |
| 15218 | Bulk Product 7f99520a | Toys & Games | $790.23 | 440 | ★ 1.0 | 20 | BULK-6d04bc12 |
| 15219 | Bulk Product 850f4488 | Toys & Games | $261.66 | 77 | ★ 4.5 | 16 | BULK-1004b136 |
| 15220 | Bulk Product ed7d4edf | Clothing | $561.70 | 317 | ★ 1.5 | 8 | BULK-70017955 |
| 15221 | Bulk Product e8bc18e4 | Electronics | $270.50 | 191 | ★ 4.0 | 80 | BULK-41a5c65a |
| 15222 | Bulk Product 4d3807f7 | Toys & Games | $868.11 | 107 | ★ 2.8 | 30 | BULK-9aa09d99 |
| 15223 | Bulk Product 3bf5b4a0 | Clothing | $807.68 | 132 | ★ 2.1 | 5 | BULK-dc79b8a0 |
| 15224 | Bulk Product c9966b93 | Sports & Outdoors | $773.80 | 410 | ★ 0.2 | 48 | BULK-56bb7b08 |
| 15225 | Bulk Product dc614582 | Clothing | $873.07 | 116 | ★ 2.7 | 2 | BULK-e3d8efee |
| 15226 | Bulk Product be2f8072 | Sports & Outdoors | $965.95 | 417 | ★ 4.0 | 40 | BULK-979c193b |
| 15227 | Bulk Product 0b094283 | Electronics | $203.20 | 494 | ★ 1.1 | 93 | BULK-4c08a294 |
| 15228 | Bulk Product 41b8a13a | Clothing | $11.06 | 199 | ★ 1.4 | 53 | BULK-807ed53b |
| 15229 | Bulk Product 2e40acf4 | Electronics | $933.42 | 210 | ★ 0.5 | 64 | BULK-23e47e18 |
| 15230 | Bulk Product 5a4ce5de | Home & Garden | $884.04 | 460 | ★ 2.7 | 63 | BULK-b8c253d5 |
| 15231 | Bulk Product 2710e959 | Toys & Games | $993.67 | 405 | ★ 4.3 | 24 | BULK-0d404b96 |
| 15232 | Bulk Product 553b7059 | Electronics | $505.58 | 459 | ★ 1.3 | 35 | BULK-c2420393 |
| 15233 | Bulk Product 95122b33 | Toys & Games | $118.48 | 16 | ★ 2.2 | 74 | BULK-3e65128a |
| 15234 | Bulk Product 24658c2f | Clothing | $997.22 | 266 | ★ 1.7 | 41 | BULK-64585c4e |
// 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
};
}