Skip to content

Commit

Permalink
Dev (#20)
Browse files Browse the repository at this point in the history
* Add FlagsEnumModelBinder

* Add OrderBy string expression
  • Loading branch information
7702244 authored Aug 4, 2023
1 parent 0e7be0d commit daf6875
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/MAOToolkit/Extensions/QueryableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
using System.Linq.Expressions;

using System.Reflection;

namespace MAOToolkit.Extensions
{
public static class QueryableExtensions
{
{
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> 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<T>)queryable.Provider.CreateQuery<T>(resultExpression);
}

public static IQueryable<T> WhereAny<T>(this IQueryable<T> queryable, params Expression<Func<T, bool>>[] predicates)
{
var parameter = Expression.Parameter(typeof(T));
Expand Down

0 comments on commit daf6875

Please sign in to comment.