title: How to set parameters in C# Expressions
body: <p>I am new to the expressions API in c#. Say I have an expression of two double parameters x and y. I would like to wrap this expression into another one where I can pass a double array instead of two arguments. I have tried the following (I am testing it in F#, hence the syntax)</p>
  
  <pre><code>let x = Expression.Parameter(typeof&lt;double&gt;,"x")
  let y = Expression.Parameter(typeof&lt;double&gt;,"y")
  let givenExpr = Expression.Add(x,y)
  
  // I know that I can evaluate givenExpr as follows
  let myLambda1 = Expression.Lambda&lt;Func&lt;double,double,double&gt;&gt;(givenExpr, [| x; y |]).Compile()
  // and this works
  myLambda1.Invoke(1.0,1.0)
  
  
  // But if I instead want to pass x and y in an array and then "assign" them, this doesn't work
  let inputArray = Expression.Parameter(typeof&lt;double[]&gt;,"inputs")
  let result = Expression.Parameter(typeof&lt;double&gt;,"result")
  
  let blockStatements = new List&lt;Expression&gt;()
  blockStatements.Add( Expression.Assign(x,Expression.ArrayAccess(inputArray,Expression.Constant(0))) );
  blockStatements.Add( Expression.Assign(y,Expression.ArrayAccess(inputArray,Expression.Constant(1))) );
  blockStatements.Add( Expression.Assign(result, givenExpr) );
  
  let block = Expression.Block( [| result |], blockStatements )
  
  let arrayLambda = Expression.Lambda&lt;Func&lt;double[],double&gt;&gt;(block, [|inputArray|]).Compile()
  // This blows up 
  arrayLambda.Invoke( [|1.0; 1.0|]) 
  // with 
  // System.InvalidOperationException: variable 'x' of type 'System.Double' referenced from scope '', but it is not defined
  //   at System.Linq.Expressions.Compiler.VariableBinder.Reference(ParameterExpression node, VariableStorageKind storage)
  //   at System.Linq.Expressions.Compiler.VariableBinder.VisitParameter(ParameterExpression node) ....
  </code></pre>
  
  <p>Clearly, I am not being able to bind the values to the parameters but I don't know how to fix this. Any hints/suggestions would be much appreciated! Thanks</p>
  
  <p><strong>EDIT:</strong> Basically what I want to achieve is to wrap a given function (e.g. Foo below) of n variables so that the wrapped function instead takes a single argument of type double[]. For plain methods I could do this as follows (example for 2 arguments)</p>
  
  <pre><code>double Foo( double x, double y);
  
  double WrappedFoo( double [] args ){
      double x = args[0];
      double y = args[1];
      return Foo( x, y );
  }
  </code></pre>
  
  <p>I am thinking of an expression as basically a function body (this might be incorrect!). So instead of Foo( double x, double y) say I am given an input expression (let's call it Foo_Expression) which I know has two parameters. I would like to wrap this expression in another one (the equivalent of WrappedFoo above) so that the new expression takes a single double array argument and then distributes those arguments to make a call to Foo_Expression. I am not sure if I can do this with expressions.</p>