Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 7910 | Bulk Product 208b2565 | Home & Garden | $767.61 | 44 | ★ 1.9 | 52 | BULK-683b35bd |
| 7911 | Bulk Product ee206296 | Home & Garden | $240.09 | 114 | ★ 0.4 | 39 | BULK-40b4e152 |
| 7912 | Bulk Product e3eb0b2a | Sports & Outdoors | $440.58 | 442 | ★ 0.5 | 97 | BULK-31d7a944 |
| 7913 | Bulk Product fd627db5 | Books | $820.35 | 35 | ★ 4.6 | 69 | BULK-5f47b5e1 |
| 7914 | Bulk Product 91a8084e | Clothing | $1,008.36 | 375 | ★ 2.1 | 24 | BULK-f1a3282a |
| 7915 | Bulk Product 66e89c8c | Toys & Games | $636.52 | 265 | ★ 4.6 | 46 | BULK-6a9f9a94 |
| 7916 | Bulk Product 59fa4f9a | Clothing | $620.59 | 171 | ★ 3.6 | 46 | BULK-02e4ec85 |
| 7917 | Bulk Product a16e5fa7 | Sports & Outdoors | $854.48 | 445 | ★ 1.2 | 23 | BULK-c5d22f24 |
| 7918 | Bulk Product e915bdfd | Clothing | $834.69 | 319 | ★ 2.8 | 0 | BULK-6e0df69c |
| 7919 | Bulk Product 46f155bb | Books | $753.42 | 437 | ★ 0.9 | 49 | BULK-bb7b94d6 |
| 7920 | Bulk Product 3794c8ac | Books | $116.59 | 361 | ★ 3.3 | 53 | BULK-e41475c6 |
| 7921 | Bulk Product 25eae69a | Toys & Games | $938.73 | 309 | ★ 4.6 | 76 | BULK-53a9bc1e |
| 7922 | Bulk Product 9d4f2325 | Clothing | $702.21 | 120 | ★ 2.9 | 76 | BULK-ad128918 |
| 7923 | Bulk Product 4da8b5f8 | Books | $964.49 | 36 | ★ 4.5 | 60 | BULK-61b36adf |
| 7924 | Bulk Product 1c14ee47 | Electronics | $425.69 | 157 | ★ 4.2 | 11 | BULK-2e0cdc2c |
| 7925 | Bulk Product 56b9ddba | Clothing | $450.52 | 72 | ★ 3.6 | 90 | BULK-e9f34040 |
| 7926 | Bulk Product 787cb4ac | Home & Garden | $884.10 | 243 | ★ 1.9 | 8 | BULK-cb2cc0a1 |
| 7927 | Bulk Product 3bff9b3f | Home & Garden | $997.38 | 217 | ★ 2.9 | 86 | BULK-5ec9d4e7 |
| 7928 | Bulk Product 11aecce0 | Home & Garden | $804.16 | 153 | ★ 2.9 | 15 | BULK-d3e57063 |
| 7929 | Bulk Product edfb85b4 | Toys & Games | $101.41 | 175 | ★ 0.6 | 45 | BULK-91e6a396 |
| 7930 | Bulk Product f79d89f7 | Sports & Outdoors | $967.10 | 313 | ★ 3.8 | 30 | BULK-798cfe84 |
| 7931 | Bulk Product 1f32ea0d | Electronics | $764.36 | 411 | ★ 0.9 | 91 | BULK-8d6032c0 |
| 7932 | Bulk Product 13734214 | Sports & Outdoors | $736.23 | 334 | ★ 5.0 | 86 | BULK-10d8b0b9 |
| 7933 | Bulk Product 4058fd5f | Clothing | $75.67 | 20 | ★ 3.0 | 11 | BULK-1b145034 |
| 7934 | Bulk Product c59bb4e0 | Electronics | $936.72 | 228 | ★ 3.9 | 66 | BULK-e2ad9a38 |
// 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
};
}