Use adapter.GetScalar() method, Please check the example of Aggregate functions in scalar queries in the manual's section "Using the generated code -> Field expressions and aggregates"
You should use the following overload to specify the predicate and the relation specified.
public virtual object GetScalar(
IEntityField2 field,
IExpression expressionToExecute,
AggregateFunction aggregateToApply,
IPredicate filter,
IGroupByCollection groupByClause,
IRelationCollection relations
)