private SqlExpression FilterHaving(SqlExpression havingExpression, IList<SqlExpression> aggregates, IQueryContext context) { if (havingExpression is SqlBinaryExpression) { var binary = (SqlBinaryExpression) havingExpression; var expType = binary.ExpressionType; var newLeft = FilterHaving(binary.Left, aggregates, context); var newRight = FilterHaving(binary.Right, aggregates, context); return SqlExpression.Binary(newLeft, expType, newRight); } // Not logical so determine if the expression is an aggregate or not if (havingExpression.HasAggregate(context)) { // Has aggregate functions so we must WriteByte this expression on the // aggregate list. aggregates.Add(havingExpression); var name = new ObjectName(FunctionTableName, String.Format("HAVINGAGG_{0}", aggregates.Count)); return SqlExpression.Reference(name); } return havingExpression; }