Combines multiple LINQ operations: filtering, sorting, grouping, and projection.
| Category | Price Range | Products | Avg Price | Avg Rating | Total Value |
|---|---|---|---|---|---|
| Books | Budget | 76 | $132.67 | ★ 4.50 | $2,891,997 |
| Books | Mid-Range | 97 | $374.62 | ★ 4.49 | $10,160,118 |
| Books | Premium | 160 | $771.27 | ★ 4.48 | $31,597,358 |
| Clothing | Budget | 72 | $135.27 | ★ 4.44 | $2,562,637 |
| Clothing | Mid-Range | 86 | $380.51 | ★ 4.51 | $7,619,222 |
| Clothing | Premium | 165 | $745.47 | ★ 4.48 | $31,726,352 |
| Electronics | Budget | 96 | $130.27 | ★ 4.45 | $3,217,767 |
| Electronics | Mid-Range | 79 | $380.79 | ★ 4.45 | $7,021,766 |
| Electronics | Premium | 195 | $747.45 | ★ 4.45 | $35,673,590 |
| Home & Garden | Budget | 80 | $124.02 | ★ 4.50 | $2,528,224 |
| Home & Garden | Mid-Range | 100 | $372.48 | ★ 4.50 | $8,868,685 |
| Home & Garden | Premium | 198 | $761.44 | ★ 4.48 | $37,616,118 |
| Sports & Outdoors | Budget | 80 | $131.76 | ★ 4.46 | $2,324,704 |
| Sports & Outdoors | Mid-Range | 97 | $382.23 | ★ 4.44 | $9,461,688 |
| Sports & Outdoors | Premium | 190 | $774.57 | ★ 4.48 | $36,239,281 |
| Toys & Games | Budget | 77 | $122.04 | ★ 4.44 | $2,569,989 |
| Toys & Games | Mid-Range | 80 | $368.34 | ★ 4.47 | $7,106,134 |
| Toys & Games | Premium | 181 | $756.65 | ★ 4.49 | $34,165,678 |
// This complex query combines multiple LINQ operations:
var recentDate = DateTime.UtcNow.AddMonths(-6);
var results = await _context.DemoProducts
// 1. Filtering - Recent, active, rated, in-stock products
.Where(p => p.IsActive &&
p.CreatedDate >= recentDate &&
p.Rating >= 4.0 &&
p.StockQuantity > 0)
// 2. Grouping - By category AND dynamically calculated price range
.GroupBy(p => new {
p.Category,
PriceRange = p.Price < 250 ? "Budget"
: p.Price < 500 ? "Mid-Range"
: "Premium"
})
// 3. Aggregation - Calculate statistics per group
.Select(g => new ComplexQueryResultDto
{
Category = g.Key.Category,
PriceRange = g.Key.PriceRange,
ProductCount = g.Count(),
AverageRating = g.Average(p => p.Rating),
AveragePrice = g.Average(p => p.Price),
TotalValue = g.Sum(p => p.Price * p.StockQuantity)
})
// 4. Sorting - Multi-level ordering
.OrderBy(r => r.Category)
.ThenBy(r => r.PriceRange)
.ToListAsync();