I want to use OrderBy("columnName").
I see that it is possible by writing an extension method or using reflection.
I am using NPoco and I am unable to write an extension method.
IQueryProvider<Sample> query = DbConnection.Query<Sample>();
I want to do:
var res = query.OrderByField("columnName");
I want to use something similar to this:
public static IQueryable<T> OrderByField<T>(this IQueryable<T> q, string SortField, bool Ascending)
{
var param = Expression.Parameter(typeof(T), "p");
var prop = Expression.Property(param, SortField);
var exp = Expression.Lambda(prop, param);
string method = Ascending ? "OrderBy" : "OrderByDescending";
Type[] types = new Type[] { q.ElementType, exp.Body.Type };
var mce = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
}
The above wont work since I am passing IQueryProvider instead of IQueryable.