using System; using System.Linq; using System.Linq.Expressions; namespace app.Data { public static class LinqExtension { public static IQueryable OrderByMember(this IQueryable q, string SortField) { var param = Expression.Parameter(typeof(T), "p"); var prop = Expression.Property(param, SortField); var exp = Expression.Lambda(prop, param); Type[] types = new Type[] { q.ElementType, exp.Body.Type }; var mce = Expression.Call(typeof(Queryable), "OrderBy", types, q.Expression, exp); return q.Provider.CreateQuery(mce); } public static IQueryable OrderByMemberDescending(this IQueryable q, string SortField) { var param = Expression.Parameter(typeof(T), "p"); var prop = Expression.Property(param, SortField); var exp = Expression.Lambda(prop, param); Type[] types = new Type[] { q.ElementType, exp.Body.Type }; var mce = Expression.Call(typeof(Queryable), "OrderByDescending", types, q.Expression, exp); return q.Provider.CreateQuery(mce); } } }