public static HashSet <string> Gather(Expression source) { AliasesProduced ap = new AliasesProduced(); ap.Visit(source); return(ap._aliases); }
protected override Expression VisitSelect(SelectExpression select) { bool saveIsOuterMostSelect = _isOuterMostSelect; try { _isOuterMostSelect = false; select = (SelectExpression)base.VisitSelect(select); bool hasOrderBy = select.OrderBy != null && select.OrderBy.Count > 0; if (hasOrderBy) { PrependOrderings(select.OrderBy); } bool canHaveOrderBy = saveIsOuterMostSelect; bool canPassOnOrderings = !saveIsOuterMostSelect; IEnumerable <OrderExpression> orderings = (canHaveOrderBy) ? _gatheredOrderings : null; ReadOnlyCollection <ColumnDeclaration> columns = select.Columns; if (_gatheredOrderings != null) { if (canPassOnOrderings) { HashSet <string> producedAliases = AliasesProduced.Gather(select.From); // reproject order expressions using this select’s alias so the outer select will have properly formed expressions BindResult project = RebindOrderings(_gatheredOrderings, select.Alias, producedAliases, select.Columns); _gatheredOrderings = project.Orderings; columns = project.Columns; } else { _gatheredOrderings = null; } } if (orderings != select.OrderBy || columns != select.Columns) { select = new SelectExpression(select.Alias, columns, select.From, select.Where, select.GroupBy, select.Having, orderings, select.Offset, select.Limit, select.IsDistinct); } return(select); } finally { _isOuterMostSelect = saveIsOuterMostSelect; } }