Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 24235 | Bulk Product 9f88bafe | Toys & Games | $998.54 | 348 | ★ 3.2 | 43 | BULK-86c85f50 |
| 24236 | Bulk Product d5b935f3 | Home & Garden | $810.28 | 94 | ★ 2.5 | 59 | BULK-b323678f |
| 24237 | Bulk Product 86d3173f | Books | $339.61 | 25 | ★ 0.7 | 54 | BULK-8b346968 |
| 24238 | Bulk Product 3f030904 | Electronics | $740.05 | 241 | ★ 2.6 | 84 | BULK-9d91cae7 |
| 24239 | Bulk Product 32b7a094 | Sports & Outdoors | $205.94 | 350 | ★ 3.7 | 57 | BULK-ac083fa6 |
| 24240 | Bulk Product ba4bd1e9 | Sports & Outdoors | $187.70 | 64 | ★ 3.4 | 0 | BULK-683ddd95 |
| 24241 | Bulk Product 9e0bae8c | Electronics | $850.86 | 191 | ★ 0.3 | 47 | BULK-2b962f55 |
| 24242 | Bulk Product 4af08285 | Clothing | $377.47 | 195 | ★ 1.0 | 20 | BULK-f5eb80e8 |
| 24243 | Bulk Product 49deb8ab | Electronics | $512.22 | 341 | ★ 3.6 | 30 | BULK-f9805643 |
| 24244 | Bulk Product f5b5d8fe | Books | $127.55 | 421 | ★ 0.2 | 21 | BULK-b3d596ea |
| 24245 | Bulk Product 88b9787d | Home & Garden | $571.47 | 225 | ★ 0.3 | 81 | BULK-48799089 |
| 24246 | Bulk Product d9767062 | Toys & Games | $902.87 | 344 | ★ 0.4 | 71 | BULK-5104db4a |
| 24247 | Bulk Product 57c4c6bf | Home & Garden | $564.15 | 30 | ★ 3.5 | 83 | BULK-691bd2f3 |
| 24248 | Bulk Product 1ea61eea | Books | $743.85 | 424 | ★ 2.5 | 86 | BULK-53075ba0 |
| 24249 | Bulk Product d3eba1da | Sports & Outdoors | $104.32 | 287 | ★ 0.8 | 16 | BULK-75d7e22a |
| 24250 | Bulk Product 2df1e459 | Electronics | $569.95 | 97 | ★ 1.6 | 84 | BULK-820938b4 |
| 24251 | Bulk Product 69bad8ff | Sports & Outdoors | $614.40 | 211 | ★ 1.5 | 99 | BULK-5ee338f9 |
| 24252 | Bulk Product f26e918c | Toys & Games | $259.60 | 102 | ★ 1.4 | 49 | BULK-96de34da |
| 24253 | Bulk Product 21796322 | Sports & Outdoors | $195.18 | 277 | ★ 3.7 | 3 | BULK-9f42e950 |
| 24254 | Bulk Product 8a192342 | Clothing | $187.94 | 318 | ★ 1.4 | 31 | BULK-22eca301 |
| 24255 | Bulk Product 14f3c605 | Home & Garden | $829.86 | 81 | ★ 3.3 | 55 | BULK-dc41e2b6 |
| 24256 | Bulk Product 8ba112ab | Home & Garden | $746.22 | 386 | ★ 0.2 | 88 | BULK-0fbacbef |
| 24257 | Bulk Product 06075b2c | Electronics | $444.30 | 157 | ★ 1.1 | 71 | BULK-91029418 |
| 24258 | Bulk Product f93370fa | Toys & Games | $690.72 | 58 | ★ 4.4 | 69 | BULK-f625557f |
| 24259 | Bulk Product fa79805a | Sports & Outdoors | $69.22 | 238 | ★ 0.4 | 72 | BULK-0fdb2f22 |
// 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
};
}