Core Concepts > Basic Queries

Having

Aggregated results can be filtered by using the Having method while composing a query.

IEnumerable<dynamic> maxPurchases = db.SelectMany(
        dbo.Person.Id,
        dbo.Person.FirstName, 
        dbo.Person.LastName,
        db.fx.Max(dbo.Purchase.TotalPurchaseAmount).As("MaxPurchaseAmt")
    )
    .From(dbo.Person)
    .InnerJoin(dbo.Purchase).On(dbo.Purchase.PersonId == dbo.Person.Id)
    .OrderBy(dbo.Person.LastName)
    .GroupBy(
        dbo.Person.Id, 
        dbo.Person.FirstName, 
        dbo.Person.LastName
    )
    .Having(
	    db.fx.Max(dbo.Purchase.TotalPurchaseAmount) >= 18.00
    )
    .Execute();

The having clause is discussed in greater detail in Filter Expressions.

Previous
Group By
Next
In

© 2024 dbExpression. All rights reserved.