public static class QueryableExtensions { public static IOrderedQueryable Order(this IQueryable source, string propertyName, SortDirection descending, bool anotherLevel = false) { var param = Expression.Parameter(typeof(T), string.Empty); var property = Expression.PropertyOrField(param, propertyName); var sort = Expression.Lambda(property, param); var call = Expression.Call( typeof(Queryable), (!anotherLevel ? "OrderBy" : "ThenBy") + (descending == SortDirection.Descending ? "Descending" : string.Empty), new[] { typeof(T), property.Type }, source.Expression, Expression.Quote(sort) ); return (IOrderedQueryable)source.Provider.CreateQuery(call); } }