Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 12985 | Bulk Product 17a8e83e | Sports & Outdoors | $169.78 | 377 | ★ 3.2 | 60 | BULK-68f9476d |
| 12986 | Bulk Product 805479e8 | Clothing | $996.18 | 319 | ★ 1.5 | 10 | BULK-f38e9165 |
| 12987 | Bulk Product be16e0c3 | Electronics | $295.91 | 310 | ★ 3.7 | 3 | BULK-647629ca |
| 12988 | Bulk Product 8699b6a9 | Home & Garden | $746.18 | 381 | ★ 1.9 | 58 | BULK-9c30fcd7 |
| 12989 | Bulk Product 5d6078ac | Electronics | $562.13 | 164 | ★ 4.2 | 1 | BULK-355d8498 |
| 12990 | Bulk Product 01d8e24c | Clothing | $642.19 | 433 | ★ 1.8 | 18 | BULK-26f50af7 |
| 12991 | Bulk Product dbb54350 | Books | $806.86 | 349 | ★ 3.7 | 57 | BULK-9fa4ae13 |
| 12992 | Bulk Product e75b7dfc | Electronics | $410.78 | 380 | ★ 0.0 | 75 | BULK-d2692824 |
| 12993 | Bulk Product 30e452fc | Books | $890.48 | 449 | ★ 2.7 | 31 | BULK-da0b7768 |
| 12994 | Bulk Product e2ac11b7 | Home & Garden | $648.53 | 302 | ★ 4.2 | 2 | BULK-9062fb9b |
| 12995 | Bulk Product 105660ab | Clothing | $362.72 | 398 | ★ 4.4 | 18 | BULK-04019835 |
| 12996 | Bulk Product edb649f5 | Home & Garden | $233.59 | 94 | ★ 2.8 | 36 | BULK-5a62bf5c |
| 12997 | Bulk Product 2a7dbd6e | Sports & Outdoors | $832.96 | 492 | ★ 4.9 | 55 | BULK-8c921b55 |
| 12998 | Bulk Product 5d99371c | Home & Garden | $447.98 | 144 | ★ 0.8 | 7 | BULK-85fe7e47 |
| 12999 | Bulk Product 9d8e3432 | Clothing | $866.55 | 345 | ★ 4.1 | 49 | BULK-e2e46729 |
| 13000 | Bulk Product 936f304e | Electronics | $334.65 | 282 | ★ 4.1 | 67 | BULK-158a916a |
| 13001 | Bulk Product 5969de48 | Toys & Games | $127.86 | 215 | ★ 2.5 | 90 | BULK-e061fb96 |
| 13002 | Bulk Product f85b0995 | Toys & Games | $209.52 | 19 | ★ 4.3 | 72 | BULK-57c30b68 |
| 13003 | Bulk Product 9a1862e0 | Clothing | $331.11 | 175 | ★ 3.3 | 82 | BULK-694cc62d |
| 13004 | Bulk Product 6a74ef86 | Sports & Outdoors | $811.79 | 4 | ★ 0.4 | 19 | BULK-9cfe4a3c |
| 13005 | Bulk Product 1c62ccc1 | Books | $243.04 | 320 | ★ 1.3 | 58 | BULK-cc008482 |
| 13006 | Bulk Product d19ef1cf | Electronics | $423.92 | 355 | ★ 4.1 | 64 | BULK-dd568f3e |
| 13007 | Bulk Product 79213946 | Electronics | $332.71 | 248 | ★ 0.9 | 73 | BULK-800cd7fe |
| 13008 | Bulk Product 3484329a | Electronics | $625.76 | 488 | ★ 3.2 | 37 | BULK-2861a764 |
| 13009 | Bulk Product b216d158 | Electronics | $652.70 | 39 | ★ 4.4 | 69 | BULK-ce315084 |
// 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
};
}