protected override Expression VisitSelect(SelectExpression select)
 {
     select = (SelectExpression)base.VisitSelect(select);
     if (_lookup.Contains(select.Alias))
     {
         var fields = new List<FieldDeclaration>(select.Fields);
         foreach (var ae in _lookup[select.Alias])
         {
             var name = "_$agg" + fields.Count;
             var field = new FieldDeclaration(name, ae.AggregateInGroupSelect);
             if (_map.ContainsKey(ae))
                 continue;
             _map.Add(ae, new FieldExpression(ae.AggregateInGroupSelect, ae.GroupByAlias, name));
             fields.Add(field);
         }
         return new SelectExpression(select.Alias, fields, select.From, select.Where, select.OrderBy, select.GroupBy, select.IsDistinct, select.Skip, select.Take);
     }
     return select;
 }
 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 virtual bool CompareFieldDeclaration(FieldDeclaration a, FieldDeclaration b)
 {
     return(a.Name == b.Name && Compare(a.Expression, b.Expression));
 }
 protected virtual bool CompareFieldDeclaration(FieldDeclaration a, FieldDeclaration b)
 {
     return a.Name == b.Name && Compare(a.Expression, b.Expression);
 }