Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 14235 | Bulk Product ff69ec79 | Sports & Outdoors | $1,004.25 | 192 | ★ 2.3 | 39 | BULK-c6354484 |
| 14236 | Bulk Product 4f978dc1 | Electronics | $413.53 | 73 | ★ 1.1 | 47 | BULK-2786d6ad |
| 14237 | Bulk Product a3d0fab2 | Toys & Games | $998.50 | 17 | ★ 1.2 | 44 | BULK-121abe5c |
| 14238 | Bulk Product 85e20efd | Toys & Games | $196.46 | 146 | ★ 4.4 | 14 | BULK-fd444a08 |
| 14239 | Bulk Product fa6e8c62 | Electronics | $112.35 | 79 | ★ 2.9 | 92 | BULK-2b2d70ae |
| 14240 | Bulk Product 4073b5c8 | Toys & Games | $934.81 | 242 | ★ 2.4 | 93 | BULK-8d346dd3 |
| 14241 | Bulk Product 783b1830 | Toys & Games | $839.94 | 181 | ★ 0.4 | 82 | BULK-8841ecbc |
| 14242 | Bulk Product 25c9f2fc | Clothing | $686.34 | 109 | ★ 4.8 | 73 | BULK-d3f6f6ff |
| 14243 | Bulk Product 0e926406 | Books | $591.89 | 449 | ★ 3.2 | 17 | BULK-9c13a3c7 |
| 14244 | Bulk Product 1845f40e | Home & Garden | $328.57 | 246 | ★ 0.5 | 67 | BULK-e169d4d3 |
| 14245 | Bulk Product fa134795 | Electronics | $845.51 | 411 | ★ 4.4 | 20 | BULK-0f279a44 |
| 14246 | Bulk Product cacc3eed | Books | $285.00 | 296 | ★ 0.2 | 39 | BULK-fb18e2ab |
| 14247 | Bulk Product b7128e17 | Clothing | $330.71 | 337 | ★ 1.5 | 96 | BULK-1b92a1bf |
| 14248 | Bulk Product 53a3e88c | Electronics | $178.06 | 138 | ★ 3.2 | 94 | BULK-9efa926a |
| 14249 | Bulk Product fabbd170 | Home & Garden | $871.54 | 379 | ★ 2.4 | 7 | BULK-1f7d89fc |
| 14250 | Bulk Product a30ae9dd | Toys & Games | $470.52 | 8 | ★ 2.9 | 54 | BULK-c20b806d |
| 14251 | Bulk Product 6bcc95f3 | Clothing | $60.09 | 37 | ★ 4.6 | 22 | BULK-893620ae |
| 14252 | Bulk Product d97987ea | Toys & Games | $753.00 | 475 | ★ 1.6 | 52 | BULK-48ff3f54 |
| 14253 | Bulk Product 6205c0da | Sports & Outdoors | $253.76 | 341 | ★ 2.6 | 66 | BULK-6a09e22a |
| 14254 | Bulk Product 1741a4fd | Electronics | $408.86 | 464 | ★ 2.7 | 63 | BULK-85104e49 |
| 14255 | Bulk Product 37941382 | Home & Garden | $283.30 | 21 | ★ 0.4 | 79 | BULK-c99f10dd |
| 14256 | Bulk Product de943bbd | Clothing | $384.57 | 339 | ★ 4.7 | 49 | BULK-1e17177e |
| 14257 | Bulk Product 9bb8f3f1 | Books | $847.42 | 204 | ★ 1.9 | 93 | BULK-013ce714 |
| 14258 | Bulk Product d10eaaca | Clothing | $891.42 | 186 | ★ 1.2 | 75 | BULK-e5bd4f25 |
| 14259 | Bulk Product 0ea00f5a | Clothing | $596.14 | 260 | ★ 2.4 | 25 | BULK-ca56efac |
// 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
};
}