<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>

It seems that
this
is the way to do it, now to verify that:
```
```
// ***** OrderBy(company => company) *****
// Create an expression tree that represents the expression
// 'whereCallExpression.OrderBy(company => company)'
MethodCallExpression orderByCallExpression = Expression.Call(
    typeof(Queryable),
    "OrderBy",
    new Type[] { queryableData.ElementType, queryableData.ElementType },
    whereCallExpression,
    Expression.Lambda<Func<string, string>>(pe, new ParameterExpression[] { pe }));
// ***** End OrderBy *****
```
```