Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 19985 | Bulk Product 73db7995 | Home & Garden | $543.23 | 16 | ★ 3.4 | 61 | BULK-b9b76415 |
| 19986 | Bulk Product 0937da8c | Clothing | $103.21 | 70 | ★ 2.3 | 10 | BULK-fc160ac3 |
| 19987 | Bulk Product d28052b3 | Sports & Outdoors | $423.43 | 319 | ★ 1.7 | 15 | BULK-4cd2f5df |
| 19988 | Bulk Product 8945605b | Electronics | $516.64 | 480 | ★ 3.1 | 23 | BULK-2683c605 |
| 19989 | Bulk Product d33b3a58 | Books | $796.20 | 166 | ★ 3.6 | 50 | BULK-14084116 |
| 19990 | Bulk Product 2e7541ff | Clothing | $670.09 | 185 | ★ 0.2 | 91 | BULK-02400aae |
| 19991 | Bulk Product d05a3cad | Books | $174.34 | 213 | ★ 1.0 | 58 | BULK-768cd323 |
| 19992 | Bulk Product 64785b55 | Books | $1,009.04 | 209 | ★ 4.8 | 7 | BULK-3b699207 |
| 19993 | Bulk Product 4da01c24 | Toys & Games | $160.73 | 220 | ★ 3.1 | 12 | BULK-ad5aae45 |
| 19994 | Bulk Product 56932846 | Home & Garden | $158.80 | 215 | ★ 1.9 | 70 | BULK-b8fa78aa |
| 19995 | Bulk Product f62a85d4 | Clothing | $384.80 | 342 | ★ 0.7 | 60 | BULK-c7e112b8 |
| 19996 | Bulk Product 35bb2a98 | Electronics | $89.06 | 479 | ★ 4.1 | 58 | BULK-ccb2b45c |
| 19997 | Bulk Product 1b3a2326 | Toys & Games | $502.18 | 449 | ★ 3.8 | 81 | BULK-e4d47398 |
| 19998 | Bulk Product f4c1a505 | Sports & Outdoors | $872.23 | 173 | ★ 2.5 | 14 | BULK-9c64cc05 |
| 19999 | Bulk Product 33e2f16a | Home & Garden | $145.92 | 345 | ★ 3.0 | 30 | BULK-10e5e033 |
| 20000 | Bulk Product b95c0a80 | Books | $474.37 | 176 | ★ 2.0 | 71 | BULK-3123e6f2 |
| 20001 | Bulk Product 12aa90e0 | Books | $841.15 | 411 | ★ 1.3 | 41 | BULK-9cbd87af |
| 20002 | Bulk Product c749c97a | Clothing | $577.39 | 324 | ★ 3.8 | 95 | BULK-aa019948 |
| 20003 | Bulk Product 20f31da9 | Sports & Outdoors | $254.66 | 135 | ★ 2.3 | 92 | BULK-c6734631 |
| 20004 | Bulk Product 3ef614be | Toys & Games | $633.82 | 320 | ★ 2.6 | 10 | BULK-fce067f0 |
| 20005 | Bulk Product f4e51a11 | Sports & Outdoors | $652.92 | 443 | ★ 0.3 | 5 | BULK-ad2eec88 |
| 20006 | Bulk Product a2e14d36 | Electronics | $545.21 | 427 | ★ 0.4 | 33 | BULK-596bed21 |
| 20007 | Bulk Product 06e5a4b9 | Clothing | $657.88 | 483 | ★ 4.7 | 28 | BULK-f3eb8bfa |
| 20008 | Bulk Product 24bc9de3 | Electronics | $439.33 | 452 | ★ 2.1 | 29 | BULK-c32d605d |
| 20009 | Bulk Product ad49034f | Sports & Outdoors | $934.57 | 140 | ★ 0.2 | 8 | BULK-921a1f12 |
// 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
};
}