ChangeColumns() public method

public ChangeColumns ( IEnumerable columns ) : SqlSelectExpression
columns IEnumerable
return SqlSelectExpression
        protected override Expression VisitSelect(SqlSelectExpression selectExpression)
        {
            List<SqlColumnDeclaration> newColumns = null;

            for (var i = 0; i < selectExpression.Columns.Count; i++)
            {
                var column = selectExpression.Columns[i];
                var visitedColumnExpression = this.Visit(column.Expression);

                if (visitedColumnExpression.Type.GetUnwrappedNullableType() == typeof(bool) && !(visitedColumnExpression is BitBooleanExpression))
                {
                    if (newColumns == null)
                    {
                        newColumns = new List<SqlColumnDeclaration>(selectExpression.Columns.Take(i));
                    }

                    var newColumnExpression = BitBooleanExpression.Coerce(visitedColumnExpression);
                    var newColumnDeclaration = new SqlColumnDeclaration(column.Name, newColumnExpression);

                    newColumns.Add(newColumnDeclaration);
                }
                else if (visitedColumnExpression != column.Expression)
                {
                    if (newColumns == null)
                    {
                        newColumns = new List<SqlColumnDeclaration>(selectExpression.Columns.Take(i));
                    }

                    newColumns.Add(column.ReplaceExpression(visitedColumnExpression));
                }
                else if (newColumns != null)
                {
                    newColumns.Add(column);
                }
            }

            var where = this.Visit(selectExpression.Where);

            if ((where is BitBooleanExpression))
            {
                where = Expression.Equal(where, Expression.Constant(true));
            }

            if (where != selectExpression.Where)
            {
                if (newColumns != null)
                {
                    return selectExpression.ChangeWhereAndColumns(where, new ReadOnlyList<SqlColumnDeclaration>(newColumns));
                }
                else
                {
                    return selectExpression.ChangeWhere(where);
                }
            }
            else if (newColumns != null)
            {
                return selectExpression.ChangeColumns(newColumns, true);
            }
            else
            {
                return base.VisitSelect(selectExpression);
            }
        }
        protected override Expression VisitSelect(SqlSelectExpression select)
        {
            var columnRemoved = false;

            select = (SqlSelectExpression)base.VisitSelect(select);

            var columnsOrderedByName = select.Columns.OrderBy(c => c.Name).ToList();

            var removedColumns = new BitArray(select.Columns.Count);

            for (int i = 0, n = columnsOrderedByName.Count; i < n - 1; i++)
            {
                var icolumn = columnsOrderedByName[i];
                var iNewColumnExpression = new SqlColumnExpression(icolumn.Expression.Type, select.Alias, icolumn.Name);

                for (var j = i + 1; j < n; j++)
                {
                    if (!removedColumns.Get(j))
                    {
                        var jcolumn = columnsOrderedByName[j];

                        if (IsSameExpression(icolumn.Expression, jcolumn.Expression))
                        {
                            // 'j' references should now be a reference to 'i'

                            var jNewColumnExpression = new SqlColumnExpression(jcolumn.Expression.Type, select.Alias, jcolumn.Name);
                            this.visitedColumns.Add(jNewColumnExpression, iNewColumnExpression);

                            removedColumns.Set(j, true);
                            columnRemoved = true;
                        }
                    }
                }
            }

            if (columnRemoved)
            {
                var newColumnDeclarations = new List<SqlColumnDeclaration>();

                for (int i = 0, n = columnsOrderedByName.Count; i < n; i++)
                {
                    if (!removedColumns.Get(i))
                    {
                        newColumnDeclarations.Add(columnsOrderedByName[i]);
                    }
                }

                select = select.ChangeColumns(newColumnDeclarations);
            }

            return select;
        }