Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 23160 | Bulk Product 7e3b23b0 | Sports & Outdoors | $810.12 | 393 | ★ 0.0 | 47 | BULK-ce0a5568 |
| 23161 | Bulk Product 7feb400d | Clothing | $688.81 | 259 | ★ 3.3 | 64 | BULK-df564382 |
| 23162 | Bulk Product 62e61184 | Clothing | $618.06 | 195 | ★ 3.0 | 36 | BULK-7a738381 |
| 23163 | Bulk Product 746ccf3e | Sports & Outdoors | $790.82 | 124 | ★ 2.3 | 14 | BULK-1c6ce9f9 |
| 23164 | Bulk Product dc233b65 | Home & Garden | $198.41 | 349 | ★ 0.2 | 13 | BULK-1d793b8f |
| 23165 | Bulk Product 2b97ee58 | Electronics | $848.43 | 444 | ★ 2.8 | 76 | BULK-714eb07a |
| 23166 | Bulk Product 21ddbc13 | Toys & Games | $684.95 | 104 | ★ 1.0 | 80 | BULK-69d005d4 |
| 23167 | Bulk Product 093b1938 | Toys & Games | $209.88 | 414 | ★ 1.0 | 48 | BULK-3e573d86 |
| 23168 | Bulk Product c10574b1 | Home & Garden | $708.23 | 55 | ★ 0.1 | 0 | BULK-476f286f |
| 23169 | Bulk Product 78516698 | Clothing | $823.48 | 323 | ★ 2.6 | 4 | BULK-8507cec3 |
| 23170 | Bulk Product fc4584de | Toys & Games | $678.94 | 221 | ★ 0.9 | 99 | BULK-fc6a9994 |
| 23171 | Bulk Product 40e5ce16 | Electronics | $95.64 | 359 | ★ 1.9 | 49 | BULK-300167e1 |
| 23172 | Bulk Product 791f5e05 | Toys & Games | $622.24 | 257 | ★ 4.9 | 34 | BULK-1eb4906d |
| 23173 | Bulk Product cad6a3d6 | Toys & Games | $990.80 | 310 | ★ 2.7 | 0 | BULK-6db55ff6 |
| 23174 | Bulk Product ad04353b | Electronics | $573.26 | 212 | ★ 1.7 | 21 | BULK-69717ee4 |
| 23175 | Bulk Product a62a75b5 | Sports & Outdoors | $124.87 | 495 | ★ 0.6 | 34 | BULK-87ca5519 |
| 23176 | Bulk Product 8a8b9fa8 | Books | $591.46 | 369 | ★ 2.5 | 19 | BULK-a04ca506 |
| 23177 | Bulk Product 4295cc87 | Toys & Games | $739.16 | 271 | ★ 3.5 | 39 | BULK-fd85fb47 |
| 23178 | Bulk Product 91caf3f7 | Home & Garden | $560.93 | 134 | ★ 0.9 | 8 | BULK-44350661 |
| 23179 | Bulk Product 376ffcaf | Electronics | $327.24 | 229 | ★ 4.1 | 28 | BULK-5a9c6aee |
| 23180 | Bulk Product 9be2a5e4 | Books | $756.43 | 212 | ★ 2.1 | 37 | BULK-e144ce57 |
| 23181 | Bulk Product ccd1d594 | Toys & Games | $216.17 | 82 | ★ 0.1 | 51 | BULK-8f94e8ad |
| 23182 | Bulk Product 9158ef77 | Clothing | $359.84 | 104 | ★ 2.8 | 47 | BULK-3df58d36 |
| 23183 | Bulk Product a3649c70 | Home & Garden | $780.31 | 213 | ★ 2.4 | 13 | BULK-5125af49 |
| 23184 | Bulk Product 6fe3dd2a | Electronics | $833.89 | 291 | ★ 0.6 | 95 | BULK-aa442769 |
// 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
};
}