Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 23910 | Bulk Product c646f183 | Clothing | $383.96 | 106 | ★ 0.7 | 67 | BULK-a22b1f04 |
| 23911 | Bulk Product 121dacfc | Home & Garden | $638.81 | 85 | ★ 2.2 | 80 | BULK-fa8cc59b |
| 23912 | Bulk Product 796523d3 | Sports & Outdoors | $318.95 | 468 | ★ 4.2 | 5 | BULK-8f3aa5fa |
| 23913 | Bulk Product 5128b64d | Clothing | $337.96 | 259 | ★ 1.4 | 85 | BULK-e3a6fb65 |
| 23914 | Bulk Product ec1aa5f2 | Home & Garden | $369.81 | 470 | ★ 4.8 | 91 | BULK-2ab7df55 |
| 23915 | Bulk Product c5cb1782 | Books | $222.88 | 267 | ★ 4.2 | 45 | BULK-c7dc800c |
| 23916 | Bulk Product deff3d95 | Clothing | $553.85 | 45 | ★ 1.7 | 0 | BULK-0259a233 |
| 23917 | Bulk Product 422e81b7 | Electronics | $922.35 | 438 | ★ 3.3 | 62 | BULK-16afa8ff |
| 23918 | Bulk Product 7b02a269 | Sports & Outdoors | $750.99 | 388 | ★ 4.3 | 8 | BULK-b22fab07 |
| 23919 | Bulk Product 32712a8f | Books | $331.09 | 311 | ★ 0.8 | 42 | BULK-bc202f4e |
| 23920 | Bulk Product 2ff96a3d | Electronics | $308.02 | 92 | ★ 3.5 | 0 | BULK-34a9e1f8 |
| 23921 | Bulk Product a63eb82b | Toys & Games | $592.99 | 370 | ★ 4.5 | 40 | BULK-82e188e4 |
| 23922 | Bulk Product 76b956b9 | Electronics | $137.25 | 209 | ★ 4.9 | 96 | BULK-05627975 |
| 23923 | Bulk Product 52731524 | Electronics | $919.51 | 75 | ★ 4.1 | 25 | BULK-34f9dc6f |
| 23924 | Bulk Product 68d52d5f | Home & Garden | $371.43 | 138 | ★ 2.1 | 5 | BULK-6eb62f3e |
| 23925 | Bulk Product 02386fd2 | Books | $632.20 | 173 | ★ 3.0 | 25 | BULK-59dad7b0 |
| 23926 | Bulk Product 732a4ce6 | Books | $107.50 | 301 | ★ 2.0 | 73 | BULK-385d18c8 |
| 23927 | Bulk Product e8752ede | Books | $481.89 | 435 | ★ 3.8 | 10 | BULK-36909b24 |
| 23928 | Bulk Product 450a191a | Electronics | $894.54 | 102 | ★ 2.5 | 59 | BULK-077f0f3e |
| 23929 | Bulk Product 6b089768 | Toys & Games | $698.71 | 354 | ★ 0.0 | 47 | BULK-b54b9b96 |
| 23930 | Bulk Product 4002559f | Sports & Outdoors | $216.41 | 201 | ★ 4.9 | 40 | BULK-a02056cd |
| 23931 | Bulk Product 3e6a9ebe | Electronics | $673.44 | 149 | ★ 1.5 | 26 | BULK-3ed27846 |
| 23932 | Bulk Product d7d8e6f4 | Home & Garden | $982.39 | 318 | ★ 0.6 | 16 | BULK-eddc7679 |
| 23933 | Bulk Product d12a65bb | Toys & Games | $626.89 | 287 | ★ 1.6 | 18 | BULK-c9f53cb6 |
| 23934 | Bulk Product 53c1ba22 | Sports & Outdoors | $400.24 | 11 | ★ 1.2 | 81 | BULK-50d1f5d5 |
// 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
};
}