title: Foreach loop using Expression trees
body: <p>I have seen this <a href="https://stackoverflow.com/questions/3646283/issue-while-building-dynamic-expression-tree">Issue while building dynamic Expression Tree</a> and  <a href="https://stackoverflow.com/questions/1936525/expression-statement-trees">Expression/Statement trees</a> and since I am new to expression trees I am still struggling to understand how to achieve what I want.</p>
  
  <p>A contrived object is below</p>
  
  <pre><code>    public class TestObject
      {
          public TestObject()
          {
              ClassList = new List&lt;Class&gt;();
          }
          public int Age { get; set; }
          public List&lt;Class&gt; ClassList { get; set; } 
      }
  
      public class Class
      {
          public string Name { get; set; }
          public int ClassId { get; set; }
      }
  </code></pre>
  
  <p>At run time I iterate through each of the properties and generate a Delegate which does a conversion to string of that property. I have got all that working. The issue I have to deal with now is that for the List type, I need to be able to apply a set of actions to each item in the ClassList property so I need a foreach which allows me to do that. </p>
  
  <p>I currently have this </p>
  
  <pre><code>//type==TestObject at runtime
  //propertyName == "ClassList"
     ParameterExpression recordExpression = Expression.Parameter(type, "record");
  
     memberExpression = MemberExpression.Property(recordExpression, propertyName);
  
     Type getEnumerableDelegateType =
                  typeof(Func&lt;,&gt;).MakeGenericType(new Type[] { type, memberExpression.Type}); 
  
     var getList = Expression.Lambda(getEnumerableDelegateType, memberExpression, recordExpression);
  </code></pre>
  
  <p>GetList when compiled and invoked returns the List as expected. What I m struggling with is how to create an expression which will use the result from the lambda expression and iterate over it applying the set of actions I have already created for each Class item.</p>
  
  <p>Ultimately I am looking for a lambda signature to match the <strong>overallAction</strong> signature below</p>
  
  <pre><code>   var getListFunc = new Func&lt;TestObject, List&lt;Class&gt;&gt;((TestObject obj1) =&gt; obj1.ClassList);
  
     Action&lt;List&lt;Class&gt;&gt; listAction = delegate(List&lt;Class&gt; data)
                  {
                      foreach (var dataChannelWithUnitse in data)
                      {
                          //Apply generated delegate
                      }
                  };
  
       Action&lt;TestObject&gt; overallAction = delegate(TestObject data)
                  {
                      var x = getListFunc.Invoke(data);
                      listAction.Invoke(x as List&lt;Class&gt;);
                  };
  </code></pre>
  
  <p>Any help is appreciated to help me understand how to do this.</p>
  
  <p>I have currently got this which is exceptioning with <strong>variable 'Input' of type 'TestObject' referenced from scope '', but it is not defined</strong></p>
  
  <pre><code>    var typeParam = Expression.Parameter(type, "Input");
      var listVariable = Expression.Variable(memberExpression.Type, "List");
      var enumerator = Expression.Variable(typeof(IEnumerator&lt;&gt;).MakeGenericType(dataType));
  
  
      var enumeratorType = typeof(IEnumerator&lt;&gt;).MakeGenericType(dataType);
      var enumerableType = typeof(IEnumerable&lt;&gt;).MakeGenericType(dataType);
      var enumerableParam = Expression.Parameter(enumerableType, "ExtractedCollection");
  
      var getEnumeratorFunc = Expression.Call(enumerableParam, enumerableType.GetMethod("GetEnumerator"));
      var getEnumeratorLambda = Expression.Lambda(getEnumeratorFunc, enumerableParam);
  
      var t1 = Expression.Assign(listVariable, Expression.Invoke(getListLambda, typeParam));
      var t2 = Expression.Assign(enumerator, Expression.Invoke(getEnumeratorLambda, listVariable));
  
  
      var @break = Expression.Label();
  
      var funcBlock = Expression.Block(
          new ParameterExpression[] { listVariable, enumerator},
  
     t1,
     t2,
  
      Expression.Loop(
          Expression.IfThenElse(
  
              Expression.NotEqual(Expression.Call(enumerator,typeof(IEnumerator).GetMethod("MoveNext")),Expression.Constant(false)),
                                  Expression.Invoke(enumerableExpressions[0],Expression.Property(enumerator, "Current")),
  
                        Expression.Break(@break))
              , @break), typeParam);
  
  
  
      Expression&lt;Action&lt;TestObject&gt;&gt; lm = Expression.Lambda&lt;Action&lt;TestObject&gt;&gt;(funcBlock,recordExpression);
      var d = lm.Compile(); **//this is exceptioning with " variable 'Input' of type 'TestObject' referenced from scope '', but it is not defined**
  </code></pre>