public static SelectExpression RemoveColumn(this SelectExpression select, ColumnDeclaration column) { List <ColumnDeclaration> columns = new List <ColumnDeclaration>(select.Columns); columns.Remove(column); return(select.SetColumns(columns)); }
protected override Expression VisitSelect(SelectExpression select) { select = (SelectExpression)base.VisitSelect(select); // look for redundant column declarations List <ColumnDeclaration> cols = select.Columns.OrderBy(c => c.Name).ToList(); BitArray removed = new BitArray(select.Columns.Count); bool anyRemoved = false; for (int i = 0, n = cols.Count; i < n - 1; i++) { ColumnDeclaration ci = cols[i]; ColumnExpression cix = ci.Expression as ColumnExpression; SqlType qt = cix != null ? cix.SqlType : ci.SqlType; ColumnExpression cxi = new ColumnExpression(ci.Expression.Type, qt, select.Alias, ci.Name); for (int j = i + 1; j < n; j++) { if (!removed.Get(j)) { ColumnDeclaration cj = cols[j]; if (SameExpression(ci.Expression, cj.Expression)) { // any reference to 'j' should now just be a reference to 'i' ColumnExpression cxj = new ColumnExpression(cj.Expression.Type, qt, select.Alias, cj.Name); this.map.Add(cxj, cxi); removed.Set(j, true); anyRemoved = true; } } } } if (anyRemoved) { List <ColumnDeclaration> newDecls = new List <ColumnDeclaration>(); for (int i = 0, n = cols.Count; i < n; i++) { if (!removed.Get(i)) { newDecls.Add(cols[i]); } } select = select.SetColumns(newDecls); } return(select); }
protected override Expression VisitJoin(JoinExpression join) { join = (JoinExpression)base.VisitJoin(join); if (join.Join == JoinType.CrossApply || join.Join == JoinType.OuterApply) { if (join.Right is TableExpression) { return(new JoinExpression(JoinType.CrossJoin, join.Left, join.Right, null)); } else { SelectExpression select = join.Right as SelectExpression; // Only consider rewriting cross apply if // 1) right side is a select // 2) other than in the where clause in the right-side select, no left-side declared aliases are referenced // 3) and has no behavior that would change semantics if the where clause is removed (like groups, aggregates, take, skip, etc). // Note: it is best to attempt this after redundant subqueries have been removed. if (select != null && select.Take == null && select.Skip == null && !AggregateChecker.HasAggregates(select) && (select.GroupBy == null || select.GroupBy.Count == 0)) { SelectExpression selectWithoutWhere = select.SetWhere(null); HashSet <TableAlias> referencedAliases = ReferencedAliasGatherer.Gather(selectWithoutWhere); HashSet <TableAlias> declaredAliases = TableAliasGatherer.Gather(join.Left); referencedAliases.IntersectWith(declaredAliases); if (referencedAliases.Count == 0) { Expression where = select.Where; select = selectWithoutWhere; var pc = ColumnProjector.ProjectColumns(where, select.Columns, select.Alias, TableAliasGatherer.Gather(select.From)); select = select.SetColumns(pc.Columns); where = pc.Projector; JoinType jt = (where == null) ? JoinType.CrossJoin : (join.Join == JoinType.CrossApply ? JoinType.InnerJoin : JoinType.LeftOuter); return(new JoinExpression(jt, join.Left, select, where)); } } } } return(join); }