protected override Expression VisitNewExpression(NewExpression expression) { if (expression.CanReduce) { return(expression.Reduce()); } int len = expression.Arguments.Count; for (int i = 0; i < len; i++) { var arg = expression.Arguments[i]; var memb = expression.Members[i]; VisitExpression(arg); var last = Query.Selects[Query.Selects.Count - 1]; if (last.Name != memb.Name && !last.Sql.Contains(" AS ")) { last.Name = memb.Name; if (!Query.CanQueryInMemory) { last.Sql += " AS \"{0}\"".With(memb.Name); } } } return(Expression.New(expression.Constructor, expression.Arguments)); }
public override Expression Reduce() { var reducedNewExpression = NewExpression.Reduce(); if (reducedNewExpression.NodeType == ExpressionType.New) { return(MemberInit( (NewExpression)reducedNewExpression, from i in Enumerable.Range(0, Arguments.Count) let a = Arguments[i] let m = WritableMembers[i] select Bind(m, a))); } else { var variable = Variable(reducedNewExpression.Type, "instance"); var expressions = new Expression[Arguments.Count + 2]; expressions[0] = Assign(variable, reducedNewExpression); for (var i = 0; i < Arguments.Count; i++) { expressions[i + 1] = Assign(MakeMemberAccess(variable, WritableMembers[i]), Arguments[i]); } expressions[Arguments.Count + 1] = Convert(variable, Type); return(Block(new[] { variable }, expressions)); } }
protected override Expression VisitNewExpression(NewExpression expression) { if (expression.CanReduce) { return(expression.Reduce()); } foreach (var arg in expression.Arguments) { VisitExpression(arg); } return(Expression.New(expression.Constructor, expression.Arguments)); }