Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 15935 | Bulk Product fc628cbc | Home & Garden | $158.51 | 331 | ★ 3.2 | 66 | BULK-a6749b2f |
| 15936 | Bulk Product bec1a3af | Electronics | $652.03 | 231 | ★ 2.6 | 81 | BULK-0cec2f42 |
| 15937 | Bulk Product 8f25296d | Books | $548.23 | 83 | ★ 4.1 | 36 | BULK-ddfcd36d |
| 15938 | Bulk Product 82b49db3 | Home & Garden | $65.33 | 71 | ★ 2.8 | 3 | BULK-e8171a12 |
| 15939 | Bulk Product 115c1daf | Books | $969.52 | 373 | ★ 2.3 | 36 | BULK-3d069463 |
| 15940 | Bulk Product eeed37e8 | Home & Garden | $536.49 | 159 | ★ 1.8 | 30 | BULK-ea8780ae |
| 15941 | Bulk Product 882caa35 | Toys & Games | $714.05 | 384 | ★ 4.6 | 94 | BULK-0920913e |
| 15942 | Bulk Product 0529b7ff | Books | $776.35 | 443 | ★ 2.6 | 48 | BULK-6a4b11ab |
| 15943 | Bulk Product 2dcdd3a8 | Toys & Games | $544.94 | 389 | ★ 1.0 | 99 | BULK-70077b65 |
| 15944 | Bulk Product 05f638bf | Electronics | $507.29 | 457 | ★ 1.4 | 54 | BULK-bc5b54bf |
| 15945 | Bulk Product 1be151b4 | Clothing | $101.14 | 325 | ★ 0.4 | 90 | BULK-a0a16025 |
| 15946 | Bulk Product cd0414dc | Clothing | $130.12 | 66 | ★ 3.4 | 79 | BULK-65c22000 |
| 15947 | Bulk Product b01980c0 | Clothing | $377.19 | 239 | ★ 2.8 | 46 | BULK-e6257dec |
| 15948 | Bulk Product cddfeb25 | Home & Garden | $851.09 | 379 | ★ 1.1 | 65 | BULK-8c2eba02 |
| 15949 | Bulk Product c9e73028 | Toys & Games | $767.23 | 498 | ★ 3.4 | 43 | BULK-2435a35d |
| 15950 | Bulk Product 2f66036a | Sports & Outdoors | $295.01 | 418 | ★ 1.6 | 5 | BULK-848d8dfa |
| 15951 | Bulk Product c2e874a6 | Toys & Games | $680.60 | 402 | ★ 0.1 | 18 | BULK-6e9c7a1d |
| 15952 | Bulk Product 64af6c78 | Books | $790.63 | 294 | ★ 0.9 | 10 | BULK-21599ff6 |
| 15953 | Bulk Product 1d407464 | Home & Garden | $185.80 | 390 | ★ 3.2 | 48 | BULK-97161e29 |
| 15954 | Bulk Product 54fe3853 | Clothing | $265.81 | 499 | ★ 2.1 | 81 | BULK-eb6ea862 |
| 15955 | Bulk Product 449d3488 | Toys & Games | $21.95 | 225 | ★ 1.8 | 6 | BULK-21fa234f |
| 15956 | Bulk Product 3c7a0492 | Toys & Games | $960.66 | 158 | ★ 1.7 | 54 | BULK-11933d3c |
| 15957 | Bulk Product a7c80e3e | Clothing | $183.40 | 453 | ★ 3.8 | 99 | BULK-b07b56af |
| 15958 | Bulk Product 09cd135c | Electronics | $493.11 | 413 | ★ 0.3 | 49 | BULK-fb404460 |
| 15959 | Bulk Product 6ceb05dd | Books | $30.41 | 264 | ★ 2.6 | 40 | BULK-7025bf03 |
// 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
};
}