Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 23135 | Bulk Product 02569660 | Sports & Outdoors | $944.20 | 293 | ★ 2.5 | 19 | BULK-7fc6e503 |
| 23136 | Bulk Product 7f692f90 | Books | $26.85 | 38 | ★ 1.7 | 3 | BULK-37debc49 |
| 23137 | Bulk Product c63bc55b | Sports & Outdoors | $157.68 | 64 | ★ 1.1 | 2 | BULK-59d48492 |
| 23138 | Bulk Product 6de9bfde | Sports & Outdoors | $96.47 | 226 | ★ 3.4 | 39 | BULK-77536ec7 |
| 23139 | Bulk Product 34c879f3 | Electronics | $384.18 | 474 | ★ 1.9 | 59 | BULK-5ea8abed |
| 23140 | Bulk Product 94104bac | Clothing | $29.20 | 269 | ★ 5.0 | 53 | BULK-b741468f |
| 23141 | Bulk Product b62df384 | Toys & Games | $183.83 | 189 | ★ 0.0 | 10 | BULK-73ac60c2 |
| 23142 | Bulk Product eca8cf80 | Sports & Outdoors | $573.20 | 18 | ★ 4.2 | 14 | BULK-fca19e42 |
| 23143 | Bulk Product e7e91e3c | Toys & Games | $367.91 | 206 | ★ 2.5 | 31 | BULK-807a31da |
| 23144 | Bulk Product d2294ba2 | Books | $432.82 | 126 | ★ 1.8 | 81 | BULK-b58f2691 |
| 23145 | Bulk Product a4fe669b | Sports & Outdoors | $491.13 | 44 | ★ 4.1 | 34 | BULK-c8eba057 |
| 23146 | Bulk Product cc001d0d | Clothing | $167.07 | 70 | ★ 3.7 | 17 | BULK-d5ca9e5d |
| 23147 | Bulk Product c8953ce3 | Books | $885.60 | 192 | ★ 5.0 | 96 | BULK-33bdd3d4 |
| 23148 | Bulk Product abaa9ebc | Books | $661.92 | 429 | ★ 4.4 | 43 | BULK-60b3a068 |
| 23149 | Bulk Product 68b61c88 | Toys & Games | $845.49 | 292 | ★ 4.6 | 26 | BULK-ea51e120 |
| 23150 | Bulk Product 1d50277c | Electronics | $220.85 | 126 | ★ 2.5 | 35 | BULK-13261e83 |
| 23151 | Bulk Product 6ba8d731 | Toys & Games | $705.39 | 465 | ★ 4.9 | 9 | BULK-b15041c2 |
| 23152 | Bulk Product 8890651f | Home & Garden | $286.29 | 260 | ★ 4.1 | 50 | BULK-56992dfd |
| 23153 | Bulk Product ebaa1458 | Sports & Outdoors | $503.75 | 377 | ★ 0.6 | 90 | BULK-e21630a9 |
| 23154 | Bulk Product 81e135b4 | Toys & Games | $376.49 | 321 | ★ 3.0 | 45 | BULK-763516e2 |
| 23155 | Bulk Product 36b5255a | Sports & Outdoors | $66.54 | 180 | ★ 4.7 | 47 | BULK-3c2010d9 |
| 23156 | Bulk Product 7ff4e4ba | Clothing | $539.51 | 441 | ★ 5.0 | 37 | BULK-fa1a9642 |
| 23157 | Bulk Product cc7bdee0 | Books | $569.42 | 162 | ★ 2.3 | 76 | BULK-eddb7b4d |
| 23158 | Bulk Product f462d41b | Electronics | $170.11 | 280 | ★ 1.5 | 75 | BULK-885eb68c |
| 23159 | Bulk Product 845cd0a6 | Clothing | $447.23 | 241 | ★ 0.5 | 85 | BULK-3100754b |
// 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
};
}