public static SelectExpression RemoveField(this SelectExpression select, FieldDeclaration field)
        {
            var fields = new List <FieldDeclaration>(select.Fields);

            fields.Remove(field);
            return(select.SetFields(fields));
        }
        public static SelectExpression AddField(this SelectExpression select, FieldDeclaration field)
        {
            var fields = new List <FieldDeclaration>(select.Fields)
            {
                field
            };

            return(select.SetFields(fields));
        }
        protected override Expression VisitSelect(SelectExpression select)
        {
            select = (SelectExpression)base.VisitSelect(select);

            var fields = select.Fields.OrderBy(f => f.Name).ToList();
            var removed = new BitArray(fields.Count);
            var anyRemoved = false;
            for (int i = 0, n = fields.Count; i < n; i++)
            {
                var fi = fields[i];
                var fxi = new FieldExpression(fi.Expression, select.Alias, fi.Name);
                for (int j = i + 1; j < n; j++)
                {
                    if (!removed.Get(i))
                    {
                        FieldDeclaration fj = fields[j];
                        if (AreSameExpression(fi.Expression, fj.Expression))
                        {
                            var fxj = new FieldExpression(fj.Expression, select.Alias, fj.Name);
                            _map.Add(fxj, fxi);
                            removed.Set(j, true);
                            anyRemoved = true;
                        }
                    }
                }
            }

            if (anyRemoved)
            {
                var newFields = new List<FieldDeclaration>();
                for (int i = 0, n = fields.Count; i < n; i++)
                {
                    if (!removed.Get(i))
                        newFields.Add(fields[i]);
                }
                select = select.SetFields(newFields);
            }
            return select;
        }