title: C# Expression.PropertyOrField in a foreach loop
body: <p>I'm trying to get expressions for the first level properties of a given class, through an array of strings, each one related to the property name to get the expression:</p>
  
  <pre><code>public List&lt;MemberExpression&gt; CreateMembers(string propertyPaths)
  {
      List&lt;MemberExpression&gt; test = new List&lt;MemberExpression&gt;();
  
  
  
      var propertiesPath = propertyPaths.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  
      foreach (var propertyPath in propertiesPath)
      {
          var mainParameter = Expression.Parameter(typeof(Process), "e");
          var property = Expression.PropertyOrField(mainParameter, typeof(Process), propertyPath);
          test.Add(property);
          property = null;
          mainParameter = null;
      }
  
      return test;
  }
  </code></pre>
  
  <p>I'm trying to get the public virtual properties from this class:</p>
  
  <pre><code>public class Process
  
  {
  
       public int? CompanyId { get; set; }
  
       public int? RecommendationId { get; set; }
  
       public int? DiagnosisNodeId { get; set; }
  
       [ForeignKey("DiagnosisId")]
       public virtual Diagnosis Diagnosis { get; set; }
  
       [ForeignKey("DiagnosisNodeId")]
       public virtual DiagnosisNode DiagnosisNode { get; set; }
  
       [ForeignKey("RecommendationId")]
       public virtual Recommendation Recommendation { get; set; }
  }
  </code></pre>
  
  <p>but after the first iteration the Expression.Property always throws an exception that the given property doesn't exist in the class Process.</p>
  
  <p>Can someone help me with this?</p>