protected override Expression VisitScalar(ScalarExpression scalar) { scalar = (ScalarExpression)base.VisitScalar(scalar); if (_currentGroupBys != null && _currentFrom != null) { if (scalar.Query is SelectExpression innerSelect && SourcesAreEqual(_currentFrom, innerSelect.From)) { var groupedColumns = GroupedColumnGatherer.Gather(innerSelect.Where); if (groupedColumns?.SequenceEqual(_currentGroupBys) ?? false) { var innerAggExpr = (AggregateExpression)innerSelect.Columns[0].Expression; if (innerAggExpr.Argument == null) { return(new AggregateExpression( innerAggExpr.Type, innerAggExpr.AggregateType, null, innerAggExpr.IsDistict )); } else if (innerAggExpr.Argument is ColumnExpression column) { var source = GetSource(_currentFrom, innerSelect.From, column.Alias); return(new AggregateExpression( innerAggExpr.Type, innerAggExpr.AggregateType, new ColumnExpression(column.Type, source.Alias, column.Name), innerAggExpr.IsDistict )); } } } } return(scalar); }
public static List <ColumnExpression> Gather(Expression expression) { var g = new GroupedColumnGatherer(); g.Visit(expression); return(g._columns); }