Complex Query

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

Execution Time

126 ms

Result Groups

18

Performance

Excellent

Books
Budget
Products

76

Avg Rating

4.5


Average Price
$132.67
Total Inventory Value
$2,891,997
Books
Mid-Range
Products

97

Avg Rating

4.5


Average Price
$374.62
Total Inventory Value
$10,160,118
Books
Premium
Products

160

Avg Rating

4.5


Average Price
$771.27
Total Inventory Value
$31,597,358
Clothing
Budget
Products

72

Avg Rating

4.4


Average Price
$135.27
Total Inventory Value
$2,562,637
Clothing
Mid-Range
Products

86

Avg Rating

4.5


Average Price
$380.51
Total Inventory Value
$7,619,222
Clothing
Premium
Products

165

Avg Rating

4.5


Average Price
$745.47
Total Inventory Value
$31,726,352
Electronics
Budget
Products

96

Avg Rating

4.5


Average Price
$130.27
Total Inventory Value
$3,217,767
Electronics
Mid-Range
Products

79

Avg Rating

4.5


Average Price
$380.79
Total Inventory Value
$7,021,766
Electronics
Premium
Products

195

Avg Rating

4.5


Average Price
$747.45
Total Inventory Value
$35,673,590
Home & Garden
Budget
Products

80

Avg Rating

4.5


Average Price
$124.02
Total Inventory Value
$2,528,224
Home & Garden
Mid-Range
Products

100

Avg Rating

4.5


Average Price
$372.48
Total Inventory Value
$8,868,685
Home & Garden
Premium
Products

198

Avg Rating

4.5


Average Price
$761.44
Total Inventory Value
$37,616,118
Sports & Outdoors
Budget
Products

80

Avg Rating

4.5


Average Price
$131.76
Total Inventory Value
$2,324,704
Sports & Outdoors
Mid-Range
Products

97

Avg Rating

4.4


Average Price
$382.23
Total Inventory Value
$9,461,688
Sports & Outdoors
Premium
Products

190

Avg Rating

4.5


Average Price
$774.57
Total Inventory Value
$36,239,281
Toys & Games
Budget
Products

77

Avg Rating

4.4


Average Price
$122.04
Total Inventory Value
$2,569,989
Toys & Games
Mid-Range
Products

80

Avg Rating

4.5


Average Price
$368.34
Total Inventory Value
$7,106,134
Toys & Games
Premium
Products

181

Avg Rating

4.5


Average Price
$756.65
Total Inventory Value
$34,165,678

Detailed Analysis

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
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