private Expression AggregateAndReduce(Expression expr) { var aggregator = new Aggregator(); aggregator.Visit(expr); if (aggregator.Source is DomainExpression) { return ProjectionFromAggregation(aggregator); } else { var source = Visit(aggregator.Source); var projector = aggregator.Projector; if (projector != null) { return Expression.Call( typeof(Enumerable).GetMethod("Select", new []{ typeof(IEnumerable<>), typeof(Func<,>) }).MakeGenericMethod(source.Type, projector.Type), source, projector); } else { return source; } } }