Try .NET /elinq/
Powered by Try .NET

SQL Server Correlated Subquery

var query = DbContext.Products.Query((Products p1) =>
{
    var highestPriceByCategory = SubQuery((Products p2) =>
    {
        var max = SELECT(MAX(p2.ListPrice));
        FROM(p2);
        WHERE(p1.Category == p2.Category);
        GROUP(BY(p2.CategoryId));
        return max.AsCollection();
    });

    var result = SELECT(p1);
    FROM(p1);
    WHERE(highestPriceByCategory.Contains(p1.ListPrice));

    return result;
})
                .OrderBy(p => p.CategoryId)
                .ThenBy(p => p.ProductName);

foreach (var product in query.Take(3))
    Console.WriteLine((product.ProductName, product.ListPrice, product.CategoryId));

< BACK | HOME