diff --git a/src/MAOToolkit/Extensions/QueryableExtensions.cs b/src/MAOToolkit/Extensions/QueryableExtensions.cs index 9b8da32..808fec9 100644 --- a/src/MAOToolkit/Extensions/QueryableExtensions.cs +++ b/src/MAOToolkit/Extensions/QueryableExtensions.cs @@ -1,9 +1,24 @@ using System.Linq.Expressions; - +using System.Reflection; + namespace MAOToolkit.Extensions { public static class QueryableExtensions - { + { + public static IOrderedQueryable OrderBy(this IQueryable queryable, string orderByProperty, bool desc = false) + { + string command = desc ? "OrderByDescending" : "OrderBy"; + var type = typeof(T); + var property = type.GetProperty(orderByProperty, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); + ArgumentNullException.ThrowIfNull(property); + + var parameter = Expression.Parameter(type, "p"); + var propertyAccess = Expression.MakeMemberAccess(parameter, property); + var orderByExpression = Expression.Lambda(propertyAccess, parameter); + var resultExpression = Expression.Call(typeof(Queryable), command, new Type[] { type, property.PropertyType }, queryable.Expression, Expression.Quote(orderByExpression)); + return (IOrderedQueryable)queryable.Provider.CreateQuery(resultExpression); + } + public static IQueryable WhereAny(this IQueryable queryable, params Expression>[] predicates) { var parameter = Expression.Parameter(typeof(T));