title: Convert method/properties chain into Expression Tree
body: <p>I have this classes:</p>
  
  <pre><code>public class DiagnosticDetails
  {
      public string PrimaryValue { get; set; }
      public Guid? Id { get; set; }
      public IEnumerable&lt;DiagnosticValues&gt; Values { get; set; }
  }
  
  public class DiagnosticValues
  {
      public string Type { get; set; }
      public string Value { get; set; }
  }
  </code></pre>
  
  <p>I want to construct dynamic <code>OrderBy().ThenBy()...</code> for every string in <code>Values</code> (this list can contain different amount of string) thus it should look like:</p>
  
  <pre><code>diagnosticDetails
      .OrderBy(detail =&gt; detail.Values.ElementAt(0).Value?.Trim())
      .ThenBy(detail =&gt; detail.Values.ElementAt(1).Value?.Trim())
      .ThenBy(detail =&gt; detail.Values.ElementAt(2).Value?.Trim())
      ...
  </code></pre>
  
  <p>But I can't convert this to expression tree:</p>
  
  <pre><code>var result = diagnosticDetails.OrderBy(detail =&gt; detail.Values.ElementAt(0).Value?.Trim());
  //var expressionTreeForResult = ???
  </code></pre>
  
  <p>This is my code:</p>
  
  <pre><code>   private List&lt;DiagnosticDetails&gt; SortByValue(List&lt;DiagnosticDetails&gt; diagnosticDetails)
      {
          IQueryable&lt;DiagnosticDetails&gt; queryableData = diagnosticDetails.AsQueryable();
  
          ParameterExpression pe = Expression.Parameter(typeof(DiagnosticDetails), "detail");
  
  
          //var predicate = diagnosticDetails.OrderBy(detail =&gt; detail.Values.ElementAt(0).Value?.Trim());
          //var predicateExpressionTree = ???
  
  
          MethodCallExpression orderByCallExpression = Expression.Call(
              typeof(Queryable),
              "OrderBy",
              new Type[] { queryableData.ElementType },
              queryableData.Expression,
              Expression.Lambda&lt;Func&lt;DiagnosticDetails, string&gt;&gt;(predicateExpressionTree, new ParameterExpression[] { pe }));
  
          var maxElementsInValues = diagnosticDetails.Select(dd =&gt; dd.Values.Count()).Max();
  
          for (int i = 1; i &lt; maxElementsInValues; i++)
          {
              orderByCallExpression = Expression.Call(
                  typeof(Queryable),
                  "ThenBy",
                  new Type[] { queryableData.ElementType },
                  orderByCallExpression,
                  Expression.Lambda&lt;Func&lt;DiagnosticDetails, string&gt;&gt;(predicateExpressionTree, new ParameterExpression[] { pe }));
          }
  
          var sortedDiagnosticDetails = Expression.Lambda&lt;List&lt;DiagnosticDetails&gt;&gt;(orderByCallExpression).Compile();
  
          return sortedDiagnosticDetails;
      }
  </code></pre>
  
  <p>How <code>.OrderBy(detail =&gt; detail.Values.ElementAt(0).Value?.Trim())</code> can be converted into expression tree?</p>