Complex Query

Combines multiple LINQ operations: filtering, sorting, grouping, and projection.

Execution Time

131 ms

Result Groups

18

Performance

Excellent

Books
Budget
Products

177

Avg Rating

4.5


Average Price
$133.88
Total Inventory Value
$6,654,277
Books
Mid-Range
Products

174

Avg Rating

4.5


Average Price
$379.41
Total Inventory Value
$17,615,885
Books
Premium
Products

352

Avg Rating

4.5


Average Price
$761.24
Total Inventory Value
$67,825,562
Clothing
Budget
Products

154

Avg Rating

4.5


Average Price
$125.02
Total Inventory Value
$5,185,639
Clothing
Mid-Range
Products

178

Avg Rating

4.5


Average Price
$375.40
Total Inventory Value
$15,907,126
Clothing
Premium
Products

335

Avg Rating

4.5


Average Price
$742.61
Total Inventory Value
$65,548,377
Electronics
Budget
Products

190

Avg Rating

4.5


Average Price
$127.86
Total Inventory Value
$5,747,613
Electronics
Mid-Range
Products

166

Avg Rating

4.5


Average Price
$378.98
Total Inventory Value
$15,635,528
Electronics
Premium
Products

357

Avg Rating

4.5


Average Price
$746.10
Total Inventory Value
$65,600,815
Home & Garden
Budget
Products

148

Avg Rating

4.5


Average Price
$122.49
Total Inventory Value
$4,819,731
Home & Garden
Mid-Range
Products

191

Avg Rating

4.5


Average Price
$370.08
Total Inventory Value
$17,676,408
Home & Garden
Premium
Products

370

Avg Rating

4.5


Average Price
$767.10
Total Inventory Value
$71,760,770
Sports & Outdoors
Budget
Products

170

Avg Rating

4.5


Average Price
$125.80
Total Inventory Value
$5,112,081
Sports & Outdoors
Mid-Range
Products

187

Avg Rating

4.5


Average Price
$376.27
Total Inventory Value
$17,544,701
Sports & Outdoors
Premium
Products

350

Avg Rating

4.5


Average Price
$768.66
Total Inventory Value
$67,100,432
Toys & Games
Budget
Products

168

Avg Rating

4.5


Average Price
$118.64
Total Inventory Value
$4,929,427
Toys & Games
Mid-Range
Products

181

Avg Rating

4.5


Average Price
$380.84
Total Inventory Value
$16,948,937
Toys & Games
Premium
Products

363

Avg Rating

4.5


Average Price
$758.42
Total Inventory Value
$69,399,713

Detailed Analysis

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
Query Breakdown
// 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();
Key Features:
  • Filtering: WHERE clause with multiple conditions (using indexed columns)
  • Dynamic Grouping: GROUP BY with calculated field (price range)
  • Aggregations: COUNT, AVG, SUM executed on SQL Server
  • Multi-level Sorting: ORDER BY with ThenBy
  • Single Query: All operations performed in one database round trip