title: No generic method 'ThenBy' on type 'System.Linq.Queryable'
body: <p>I'm trying to create a generic <strong>OrderBy</strong>(fields collection) method to use in a new EF .Net Core 2.1 and managed to write the below code:</p>
  
  <pre><code> public static Func&lt;IQueryable&lt;TEntity&gt;, IOrderedQueryable&lt;TEntity&gt;&gt; GetOrderByFunction(List&lt;SortColumn&gt; columns)
   {
              Type typeQueryable = typeof(IQueryable&lt;TEntity&gt;);
              ParameterExpression argQueryable = Expression.Parameter(typeQueryable, "p");
              LambdaExpression outerExpression = Expression.Lambda(argQueryable, argQueryable);
              Type entityType = typeof(TEntity);
  
              ParameterExpression arg = Expression.Parameter(entityType, "x");
              Expression expr = arg;
  
              Expression resultExp = null;
  
              foreach (SortColumn sc in columns)
              {
                  PropertyInfo pi = entityType.GetProperty(sc.FieldName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                  Expression propertyExpr = Expression.Property(expr, pi);
                  Type propertyType = pi.PropertyType;
                  LambdaExpression lambdaExp = Expression.Lambda(propertyExpr, arg);
                  String methodName = string.Empty;
  
                  if (resultExp != null)
                  {
                      methodName = sc.Descending ? "ThenBy" : "ThenByDescending";
  
                      /// No generic method 'ThenBy' on type 'System.Linq.Queryable' is compatible with the supplied type arguments and arguments.
                      /// No type arguments should be provided if the method is non - generic.
                      Expression exp = Expression.Call(typeof(Queryable), methodName, new Type[] { entityType, propertyType }, outerExpression.Body, Expression.Quote(lambdaExp));
  
                      MethodInfo minfo = typeof(Queryable).GetMethods(BindingFlags.Static | BindingFlags.Public).First(m =&gt; m.Name == methodName);
  
                      /// Method System.Linq.IOrderedQueryable`1
                      /// [TSource] OrderBy[TSource,TKey](System.Linq.IQueryable`1
                      /// [TSource],System.Linq.Expressions.Expression`1
                      /// [System.Func`2[TSource,TKey]]) is a generic method definition
                      /// Parameter name: method
                      resultExp = Expression.Call(minfo, exp, resultExp);
                  }
                  else
                  {
                      methodName = sc.Descending ? "OrderBy" : "OrderByDescending";
                      resultExp = Expression.Call(typeof(Queryable), methodName, new Type[] { entityType, propertyType }, outerExpression.Body, Expression.Quote(lambdaExp));
                  }
              }
  
              LambdaExpression orderedLambda = Expression.Lambda(resultExp, argQueryable);
              return (Func&lt;IQueryable&lt;TEntity&gt;, IOrderedQueryable&lt;TEntity&gt;&gt;)orderedLambda.Compile();
          }
  </code></pre>
  
  <p>My <code>Sortcolumn</code> class just hold the field name and order direction.</p>
  
  <pre><code>public class SortColumn
  {
      public string FieldName { get; }
      public bool Descending { get; }
  
      public SortColumn(string fieldName, bool descending)
      {
          FieldName = fieldName;
          Descending = descending;
      }
  }
  </code></pre>
  
  <p>It works when my collection has only a single item like Id or Description but fails to sort by more than one field.</p>
  
  <p>The errors I got are commented in the snippet:</p>
  
  <blockquote>
    <p>No generic method 'ThenBy' on type 'System.Linq.Queryable' is compatible with the supplied type arguments
    and arguments.
    No type arguments should be provided if the method is non - generic.</p>
  </blockquote>
  
  <p>It's strange because it works fine for the <code>OrderBy</code> command.
  But even if I pass the <code>OrderBy</code> I was unable to "join" the methods to assemble a unique expression. The error I got is:</p>
  
  <blockquote>
    <p>Method System.Linq.IOrderedQueryable'1 [TSource]
    OrderBy[TSource,TKey](System.Linq.IQueryable'1
    [TSource],System.Linq.Expressions.Expression'1
    [System.Func'2[TSource,TKey]]) is a generic method definition
    Parameter name: method</p>
  </blockquote>
  
  <p>Things I tried:
  Change <code>typeof(Queryable)</code> to use <code>Enumerable</code> or <code>EnumerableQuery</code> or even List, Use invoke, etc.</p>
  
  <p>My goal is to dynamicaly pass a list of fields to order my GetAll() methods for my domain objects building a delegate function can give me something like:</p>
  
  <pre><code>            var test = new List&lt;TEntity&gt;();
              test.OrderBy(x =&gt; x.Id).ThenByDescending(x =&gt; x.Description);
  </code></pre>