Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 8910 | Bulk Product e6c1fdb1 | Clothing | $427.15 | 384 | ★ 2.6 | 12 | BULK-4c065270 |
| 8911 | Bulk Product 844be354 | Books | $449.57 | 126 | ★ 0.5 | 94 | BULK-15abebe0 |
| 8912 | Bulk Product d4685d6b | Sports & Outdoors | $206.21 | 82 | ★ 0.0 | 52 | BULK-032ace8e |
| 8913 | Bulk Product d8de8b70 | Toys & Games | $99.27 | 437 | ★ 2.9 | 43 | BULK-53e86864 |
| 8914 | Bulk Product a6112280 | Toys & Games | $518.32 | 323 | ★ 0.8 | 48 | BULK-6217f4e8 |
| 8915 | Bulk Product 2cb5e323 | Books | $561.06 | 456 | ★ 3.3 | 84 | BULK-93f30df7 |
| 8916 | Bulk Product 53dae5c6 | Toys & Games | $1,006.21 | 184 | ★ 2.1 | 8 | BULK-5e9f7a25 |
| 8917 | Bulk Product 43a6d9f4 | Books | $394.53 | 53 | ★ 2.4 | 13 | BULK-df5fabdf |
| 8918 | Bulk Product 6c4b32a6 | Books | $295.49 | 419 | ★ 2.8 | 35 | BULK-7cff1a3c |
| 8919 | Bulk Product e190adc1 | Sports & Outdoors | $861.67 | 227 | ★ 4.5 | 73 | BULK-a0230a17 |
| 8920 | Bulk Product 7fa79841 | Books | $666.60 | 84 | ★ 1.4 | 36 | BULK-d1c6ff7c |
| 8921 | Bulk Product 0b673c5c | Home & Garden | $942.62 | 26 | ★ 5.0 | 8 | BULK-6ec5389a |
| 8922 | Bulk Product cf79b256 | Sports & Outdoors | $918.05 | 297 | ★ 3.6 | 10 | BULK-296b5c32 |
| 8923 | Bulk Product 9d6a064f | Electronics | $836.84 | 439 | ★ 2.2 | 0 | BULK-5f1f250f |
| 8924 | Bulk Product a22b421b | Books | $684.82 | 419 | ★ 2.4 | 31 | BULK-f6510439 |
| 8925 | Bulk Product 6cdc5a94 | Books | $894.70 | 270 | ★ 2.7 | 59 | BULK-42c6bcdf |
| 8926 | Bulk Product 81b03fe5 | Clothing | $241.98 | 5 | ★ 4.9 | 84 | BULK-ddd31125 |
| 8927 | Bulk Product d0b22834 | Home & Garden | $443.33 | 372 | ★ 5.0 | 34 | BULK-841ecc77 |
| 8928 | Bulk Product 49c24a97 | Clothing | $184.08 | 246 | ★ 4.7 | 10 | BULK-6b823aca |
| 8929 | Bulk Product bdc10c3b | Books | $847.81 | 360 | ★ 0.2 | 1 | BULK-e3f255ac |
| 8930 | Bulk Product 7f254705 | Home & Garden | $681.47 | 243 | ★ 0.6 | 44 | BULK-670f2808 |
| 8931 | Bulk Product 6f2f70df | Clothing | $362.31 | 378 | ★ 1.0 | 51 | BULK-8539ec95 |
| 8932 | Bulk Product 85f1182f | Clothing | $683.12 | 93 | ★ 4.4 | 25 | BULK-27e95365 |
| 8933 | Bulk Product f8ef2a11 | Clothing | $232.85 | 370 | ★ 2.9 | 8 | BULK-af4efed3 |
| 8934 | Bulk Product a7a4e60e | Electronics | $431.46 | 411 | ★ 1.6 | 19 | BULK-c047a7e8 |
// 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
};
}