Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 19910 | Bulk Product d553097b | Books | $832.41 | 25 | ★ 3.1 | 56 | BULK-8240771b |
| 19911 | Bulk Product 7f3907e4 | Home & Garden | $755.32 | 331 | ★ 1.5 | 91 | BULK-546b5644 |
| 19912 | Bulk Product 49d25e3f | Clothing | $650.91 | 258 | ★ 4.1 | 6 | BULK-9cb1fdc8 |
| 19913 | Bulk Product 3f06a883 | Sports & Outdoors | $447.71 | 490 | ★ 4.6 | 82 | BULK-0d138266 |
| 19914 | Bulk Product 12d605e7 | Sports & Outdoors | $825.25 | 386 | ★ 1.7 | 52 | BULK-55cb95e9 |
| 19915 | Bulk Product 5fa6f0d2 | Books | $961.42 | 81 | ★ 4.1 | 58 | BULK-8e2befe3 |
| 19916 | Bulk Product e2c39073 | Books | $322.93 | 455 | ★ 3.9 | 50 | BULK-da07fb82 |
| 19917 | Bulk Product bd5a94dc | Books | $435.78 | 402 | ★ 1.4 | 44 | BULK-9a5d439b |
| 19918 | Bulk Product 87c46ecc | Electronics | $447.77 | 5 | ★ 3.9 | 48 | BULK-56ad9ff8 |
| 19919 | Bulk Product 8a870874 | Sports & Outdoors | $935.87 | 164 | ★ 5.0 | 96 | BULK-89ca2bc9 |
| 19920 | Bulk Product c5bb8458 | Electronics | $48.93 | 115 | ★ 4.1 | 20 | BULK-29752ece |
| 19921 | Bulk Product b43b1a00 | Electronics | $49.86 | 476 | ★ 2.5 | 87 | BULK-843ad966 |
| 19922 | Bulk Product eba377ff | Home & Garden | $926.38 | 493 | ★ 4.8 | 4 | BULK-820af19e |
| 19923 | Bulk Product 0a0fcab2 | Home & Garden | $108.63 | 83 | ★ 1.0 | 25 | BULK-99eb96ad |
| 19924 | Bulk Product 331b02a5 | Electronics | $460.51 | 31 | ★ 3.1 | 77 | BULK-24820e4b |
| 19925 | Bulk Product 1fd9609e | Sports & Outdoors | $54.96 | 254 | ★ 4.6 | 8 | BULK-cb97d4d8 |
| 19926 | Bulk Product 45a81c70 | Toys & Games | $747.24 | 246 | ★ 1.7 | 56 | BULK-fd0fd9c6 |
| 19927 | Bulk Product 4bb9a4ba | Books | $114.96 | 415 | ★ 5.0 | 2 | BULK-d983287f |
| 19928 | Bulk Product 1fca1de2 | Books | $399.86 | 215 | ★ 4.6 | 88 | BULK-b8cc22fd |
| 19929 | Bulk Product 404929e4 | Home & Garden | $873.34 | 495 | ★ 3.0 | 27 | BULK-34b92d2c |
| 19930 | Bulk Product 36e22cbd | Books | $101.55 | 485 | ★ 2.4 | 48 | BULK-bcea5ea1 |
| 19931 | Bulk Product 7c83d4d7 | Electronics | $798.13 | 453 | ★ 3.6 | 34 | BULK-a979da90 |
| 19932 | Bulk Product 475ae08d | Electronics | $498.71 | 168 | ★ 1.7 | 60 | BULK-48c71803 |
| 19933 | Bulk Product d797a794 | Toys & Games | $738.13 | 127 | ★ 2.2 | 36 | BULK-49aea9a0 |
| 19934 | Bulk Product e802d2a0 | Books | $443.91 | 458 | ★ 1.9 | 68 | BULK-15939c8f |
// 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
};
}