Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 18885 | Bulk Product 2bd3dcee | Toys & Games | $94.09 | 428 | ★ 1.5 | 51 | BULK-7a14bfe3 |
| 18886 | Bulk Product 8fb2cc3d | Clothing | $841.44 | 103 | ★ 4.6 | 0 | BULK-6e45cd32 |
| 18887 | Bulk Product 0905a5c5 | Books | $625.03 | 269 | ★ 3.8 | 50 | BULK-4c1b4f8d |
| 18888 | Bulk Product a483bb72 | Clothing | $952.16 | 199 | ★ 2.2 | 56 | BULK-2bff24c1 |
| 18889 | Bulk Product fff75337 | Clothing | $729.21 | 238 | ★ 1.6 | 98 | BULK-ef7e3d0c |
| 18890 | Bulk Product 7e10259c | Electronics | $457.25 | 432 | ★ 3.0 | 37 | BULK-1407c4e0 |
| 18891 | Bulk Product 7bd477d2 | Books | $645.28 | 92 | ★ 3.5 | 3 | BULK-c476e55c |
| 18892 | Bulk Product 48315e88 | Electronics | $242.13 | 68 | ★ 4.0 | 14 | BULK-0a491f67 |
| 18893 | Bulk Product 573848ef | Toys & Games | $1,007.07 | 87 | ★ 0.5 | 57 | BULK-3a45b55f |
| 18894 | Bulk Product befda5b6 | Home & Garden | $80.19 | 250 | ★ 2.5 | 66 | BULK-f80efff8 |
| 18895 | Bulk Product 259baccf | Electronics | $530.00 | 30 | ★ 3.7 | 83 | BULK-6e9e63c7 |
| 18896 | Bulk Product d859a579 | Toys & Games | $915.66 | 356 | ★ 3.0 | 69 | BULK-e06f67e8 |
| 18897 | Bulk Product 55e81b42 | Toys & Games | $360.96 | 22 | ★ 2.5 | 3 | BULK-c5dc3f80 |
| 18898 | Bulk Product 1f27df62 | Toys & Games | $666.67 | 24 | ★ 4.0 | 57 | BULK-af492e64 |
| 18899 | Bulk Product ac9b2a7e | Sports & Outdoors | $468.40 | 315 | ★ 2.2 | 73 | BULK-5958a5b6 |
| 18900 | Bulk Product 9f412234 | Books | $222.57 | 251 | ★ 4.7 | 50 | BULK-7211b5c6 |
| 18901 | Bulk Product 550452f3 | Electronics | $824.58 | 285 | ★ 0.9 | 62 | BULK-e5dd4e15 |
| 18902 | Bulk Product fbff3b87 | Sports & Outdoors | $959.88 | 486 | ★ 0.5 | 15 | BULK-5d523d52 |
| 18903 | Bulk Product 04525757 | Sports & Outdoors | $793.57 | 276 | ★ 0.3 | 56 | BULK-97b27ea5 |
| 18904 | Bulk Product b66c7b84 | Clothing | $247.94 | 335 | ★ 3.8 | 93 | BULK-36894758 |
| 18905 | Bulk Product c6cdd026 | Home & Garden | $843.10 | 14 | ★ 2.4 | 0 | BULK-ed410961 |
| 18906 | Bulk Product dbd69472 | Toys & Games | $286.06 | 107 | ★ 4.6 | 93 | BULK-ddb294ec |
| 18907 | Bulk Product 3cf7132d | Books | $678.96 | 4 | ★ 4.8 | 94 | BULK-9fb565a7 |
| 18908 | Bulk Product 60d9e5ac | Clothing | $335.92 | 175 | ★ 0.4 | 19 | BULK-41f4ac20 |
| 18909 | Bulk Product 1fe52a3a | Sports & Outdoors | $213.66 | 359 | ★ 2.0 | 98 | BULK-b6efe3ab |
// 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
};
}