title: How do I apply OrderBy on an IQueryable using a string column name within a generic extension method?
body: <pre><code>public static IQueryable&lt;TResult&gt; ApplySortFilter&lt;T, TResult&gt;(this IQueryable&lt;T&gt; query, string columnName)
    where T : EntityObject
  {
    var param = Expression.Parameter(typeof(T), "o");
    var body = Expression.PropertyOrField(param,columnName);
  
    var sortExpression = Expression.Lambda(body, param);
    return query.OrderBy(sortExpression);
  }
  </code></pre>
  
  <p>Because the type for OrderBy is not inferred from sortExpression I need to specify it something like this at run time:</p>
  
  <pre><code>var sortExpression = Expression.Lambda&lt;T, TSortColumn&gt;(body, param);
  </code></pre>
  
  <p>Or</p>
  
  <pre><code>return query.OrderBy&lt;T, TSortColumn&gt;(sortExpression);
  </code></pre>
  
  <p>I don't think this is possible however as TSortColumn can only be determined during runtime.</p>
  
  <p>Is there a way around this?</p>