Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 13835 | Bulk Product f6e20330 | Home & Garden | $81.65 | 124 | ★ 1.5 | 72 | BULK-cffc1b73 |
| 13836 | Bulk Product 1863baee | Electronics | $979.77 | 232 | ★ 2.0 | 16 | BULK-bc5dd1dc |
| 13837 | Bulk Product 0424d9ba | Electronics | $260.03 | 292 | ★ 1.3 | 28 | BULK-c7dcf7c5 |
| 13838 | Bulk Product f2a58205 | Toys & Games | $326.87 | 249 | ★ 3.1 | 89 | BULK-92b45ef4 |
| 13839 | Bulk Product 54579672 | Books | $828.61 | 358 | ★ 4.9 | 91 | BULK-4e9e2ed7 |
| 13840 | Bulk Product 50d1fff7 | Home & Garden | $36.76 | 69 | ★ 3.6 | 33 | BULK-fe02fe6d |
| 13841 | Bulk Product ba1f99f8 | Toys & Games | $472.06 | 191 | ★ 4.7 | 6 | BULK-be234837 |
| 13842 | Bulk Product 0c982ce4 | Electronics | $416.38 | 404 | ★ 2.5 | 74 | BULK-f028869b |
| 13843 | Bulk Product e5c17c06 | Home & Garden | $700.83 | 369 | ★ 2.0 | 68 | BULK-8266e563 |
| 13844 | Bulk Product bd6a850d | Clothing | $587.84 | 99 | ★ 3.1 | 43 | BULK-517cda50 |
| 13845 | Bulk Product c7a9a4e6 | Clothing | $379.63 | 155 | ★ 1.7 | 26 | BULK-bd9ce718 |
| 13846 | Bulk Product 29c3f895 | Clothing | $149.10 | 7 | ★ 3.1 | 38 | BULK-8fa749b6 |
| 13847 | Bulk Product dbd2c922 | Books | $54.11 | 157 | ★ 0.9 | 8 | BULK-1fb4c594 |
| 13848 | Bulk Product 72b29f5d | Clothing | $262.18 | 269 | ★ 2.6 | 36 | BULK-893bd8d8 |
| 13849 | Bulk Product 1205d69e | Home & Garden | $640.34 | 271 | ★ 1.9 | 49 | BULK-8bae6927 |
| 13850 | Bulk Product dc1fd443 | Clothing | $869.69 | 431 | ★ 1.4 | 93 | BULK-44be5b4a |
| 13851 | Bulk Product 1060e5ce | Books | $724.79 | 284 | ★ 2.0 | 9 | BULK-b8ee05c4 |
| 13852 | Bulk Product ebfd4209 | Toys & Games | $385.22 | 313 | ★ 3.8 | 25 | BULK-cd31cf2c |
| 13853 | Bulk Product 45e7cd7c | Home & Garden | $486.17 | 393 | ★ 1.2 | 55 | BULK-697c5a73 |
| 13854 | Bulk Product 5ebfab6e | Books | $196.25 | 277 | ★ 2.5 | 22 | BULK-f2c25a79 |
| 13855 | Bulk Product ff1b963f | Toys & Games | $416.50 | 204 | ★ 1.5 | 80 | BULK-84471e8d |
| 13856 | Bulk Product 93177a67 | Clothing | $1,009.23 | 426 | ★ 4.1 | 10 | BULK-c6f2c526 |
| 13857 | Bulk Product a6e92b4f | Sports & Outdoors | $780.05 | 316 | ★ 3.3 | 72 | BULK-39457f8b |
| 13858 | Bulk Product 052d3504 | Clothing | $825.81 | 166 | ★ 1.0 | 81 | BULK-444753a8 |
| 13859 | Bulk Product a06d4d52 | Clothing | $24.18 | 235 | ★ 1.8 | 84 | BULK-4fa4725f |
// 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
};
}