Combines multiple LINQ operations: filtering, sorting, grouping, and projection.
| Category | Price Range | Products | Avg Price | Avg Rating | Total Value |
|---|---|---|---|---|---|
| Books | Budget | 177 | $133.88 | ★ 4.50 | $6,654,277 |
| Books | Mid-Range | 174 | $379.41 | ★ 4.46 | $17,615,885 |
| Books | Premium | 352 | $761.24 | ★ 4.49 | $67,825,562 |
| Clothing | Budget | 154 | $125.02 | ★ 4.46 | $5,185,639 |
| Clothing | Mid-Range | 178 | $375.40 | ★ 4.47 | $15,907,126 |
| Clothing | Premium | 335 | $742.61 | ★ 4.46 | $65,548,377 |
| Electronics | Budget | 190 | $127.86 | ★ 4.50 | $5,747,613 |
| Electronics | Mid-Range | 166 | $378.98 | ★ 4.45 | $15,635,528 |
| Electronics | Premium | 357 | $746.10 | ★ 4.48 | $65,600,815 |
| Home & Garden | Budget | 148 | $122.49 | ★ 4.48 | $4,819,731 |
| Home & Garden | Mid-Range | 191 | $370.08 | ★ 4.49 | $17,676,408 |
| Home & Garden | Premium | 370 | $767.10 | ★ 4.49 | $71,760,770 |
| Sports & Outdoors | Budget | 170 | $125.80 | ★ 4.46 | $5,112,081 |
| Sports & Outdoors | Mid-Range | 187 | $376.27 | ★ 4.46 | $17,544,701 |
| Sports & Outdoors | Premium | 350 | $768.66 | ★ 4.49 | $67,100,432 |
| Toys & Games | Budget | 168 | $118.64 | ★ 4.45 | $4,929,427 |
| Toys & Games | Mid-Range | 181 | $380.84 | ★ 4.48 | $16,948,937 |
| Toys & Games | Premium | 363 | $758.42 | ★ 4.48 | $69,399,713 |
// 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();