0

I have the following custom extension method, which allows you to dynamically specify which field will be sorted - it is used to tie into a JQuery DataTable.

public static IQueryable<T> OrderByField<T>(this IQueryable<T> q, string SortField, string SortDirection)
{

  var param = Expression.Parameter(typeof(T), "p");
  var prop = Expression.Property(param, SortField);
  var exp = Expression.Lambda(prop, param);

  var method = SortDirection == "asc" ? "OrderBy" : "OrderByDescending";

  var types = new Type[] { q.ElementType, exp.Body.Type };
  var mce = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);

  return q.Provider.CreateQuery<T>(mce);

}

It is then called as follows: .OrderByField("ColumnName", "asc") where the two params are strings.

I wish to modify this function to allow "thenby" to be called immediately after, however I am unsure how to do this.

Any help greatly appreciated.

Gavin Coates
  • 1,385
  • 1
  • 17
  • 40

1 Answers1

0

You can call OrderBy() or OrderByDescending() directly (not through CreateQuery()), then return the IOrderedQueryable<T>.

SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896