/// <summary> /// Repackages and passes an expression from an aggregate subquery back to the parent query. /// </summary> private SqlExpression CreateAggregateMappingExpression(SqlQuery parentQuery, SqlTable proxyTable, SqlExpression sqlExpr) { var subQuery = proxyTable.SubQuery.Queries[0]; string alias = parentQuery.AliasManager.CreateAlias("aggCol"); var groupByColumn = new SqlSelectItem { Expression = sqlExpr, Alias = alias }; subQuery.SelectClause.Items.Add(groupByColumn); subQuery.GroupByClause.Expressions.Add(groupByColumn.Expression); // Register an expression that can be used to access the group-by expression string mappedSql = GetColumnSql(proxyTable, alias); var mappedSqlExpr = new SqlExpression(mappedSql); SqlExpression.CopyTransforms(sqlExpr, mappedSqlExpr); mappedSqlExpr.DatabaseType = sqlExpr.DatabaseType; return(mappedSqlExpr); }