public static DbSelectExpression RemoveColumn(this DbSelectExpression select, DbColumnDeclaration column) { List <DbColumnDeclaration> columns = new List <DbColumnDeclaration>(select.Columns); columns.Remove(column); return(select.SetColumns(columns)); }
protected override Expression VisitJoin(DbJoinExpression join) { join = (DbJoinExpression)base.VisitJoin(join); if (join.Join == DbJoinType.CrossApply || join.Join == DbJoinType.OuterApply) { if (join.Right is DbTableExpression) { return(new DbJoinExpression(DbJoinType.CrossJoin, join.Left, join.Right, null)); } else { DbSelectExpression select = join.Right as DbSelectExpression; // 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)) { DbSelectExpression selectWithoutWhere = select.SetWhere(null); HashSet <DbTableAlias> referencedAliases = ReferencedAliasGatherer.Gather(selectWithoutWhere); HashSet <DbTableAlias> declaredAliases = DeclaredAliasGatherer.Gather(join.Left); referencedAliases.IntersectWith(declaredAliases); if (referencedAliases.Count == 0) { Expression where = select.Where; select = selectWithoutWhere; var pc = ColumnProjector.ProjectColumns(this.CanBeColumn, where, select.Columns, select.Alias, DeclaredAliasGatherer.Gather(select.From)); select = select.SetColumns(pc.Columns); where = pc.Projector; DbJoinType jt = (where == null) ? DbJoinType.CrossJoin : (join.Join == DbJoinType.CrossApply ? DbJoinType.InnerJoin : DbJoinType.LeftOuter); return(new DbJoinExpression(jt, join.Left, select, where)); } } } } return(join); }
protected override Expression VisitSelect(DbSelectExpression select) { select = (DbSelectExpression)base.VisitSelect(select); // look for redundant column declarations List <DbColumnDeclaration> 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++) { DbColumnDeclaration ci = cols[i]; DbColumnExpression cix = ci.Expression as DbColumnExpression; DbDataType qt = cix != null ? cix.DbType : null; DbColumnExpression cxi = new DbColumnExpression(ci.Expression.Type, qt, select.Alias, ci.Name); for (int j = i + 1; j < n; j++) { if (!removed.Get(j)) { DbColumnDeclaration cj = cols[j]; if (SameExpression(ci.Expression, cj.Expression)) { // any reference to 'j' should now just be a reference to 'i' DbColumnExpression cxj = new DbColumnExpression(cj.Expression.Type, qt, select.Alias, cj.Name); this.map.Add(cxj, cxi); removed.Set(j, true); anyRemoved = true; } } } } if (anyRemoved) { List <DbColumnDeclaration> newDecls = new List <DbColumnDeclaration>(); 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 VisitSelect(DbSelectExpression select) { select = (DbSelectExpression)base.VisitSelect(select); // look for redundant column declarations List<DbColumnDeclaration> 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++) { DbColumnDeclaration ci = cols[i]; DbColumnExpression cix = ci.Expression as DbColumnExpression; DbDataType qt = cix != null ? cix.DbType : null; DbColumnExpression cxi = new DbColumnExpression(ci.Expression.Type, qt, select.Alias, ci.Name); for (int j = i + 1; j < n; j++) { if (!removed.Get(j)) { DbColumnDeclaration cj = cols[j]; if (SameExpression(ci.Expression, cj.Expression)) { // any reference to 'j' should now just be a reference to 'i' DbColumnExpression cxj = new DbColumnExpression(cj.Expression.Type, qt, select.Alias, cj.Name); this.map.Add(cxj, cxi); removed.Set(j, true); anyRemoved = true; } } } } if (anyRemoved) { List<DbColumnDeclaration> newDecls = new List<DbColumnDeclaration>(); for (int i = 0, n = cols.Count; i < n; i++) { if (!removed.Get(i)) { newDecls.Add(cols[i]); } } select = select.SetColumns(newDecls); } return select; }