I wrote this code that sorts an IQueryable<T> by the column sortColumn. I would like to extend it so that the entries that have the value of the column BirthDate equal to DateTime.Today would be placed first in the sort, but I just can’t find or think of how to do the job.
public static IQueryable<T> OrderByField<T>(this IQueryable<T> q, string sortColumn, bool asc)
{
var param = Expression.Parameter(typeof(T), "p");
var prop = Expression.Property(param, sortColumn);
var exp = Expression.Lambda(prop, param);
string method = asc ? "OrderBy" : "OrderByDescending";
Type[] types = new[] { q.ElementType, exp.Body.Type };
var mce = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
return q.Provider.CreateQuery<T>(mce);
}
See Handle GridView.OnSorting() and create sorting expression dynamically using LINQ
And links from the bottom: