2018-01-27 06:20:57 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Linq.Expressions;
|
2020-11-03 10:42:41 +01:00
|
|
|
|
using System.Reflection;
|
2018-01-27 06:20:57 +01:00
|
|
|
|
|
|
|
|
|
namespace Sieve.Extensions
|
|
|
|
|
{
|
2020-10-21 18:51:15 +02:00
|
|
|
|
public static partial class LinqExtensions
|
2018-01-27 06:20:57 +01:00
|
|
|
|
{
|
2020-10-17 20:10:52 +02:00
|
|
|
|
public static IQueryable<TEntity> OrderByDynamic<TEntity>(
|
|
|
|
|
this IQueryable<TEntity> source,
|
|
|
|
|
string fullPropertyName,
|
2020-11-03 10:42:41 +01:00
|
|
|
|
PropertyInfo propertyInfo,
|
2020-10-17 20:10:52 +02:00
|
|
|
|
bool desc,
|
|
|
|
|
bool useThenBy)
|
2018-01-27 06:20:57 +01:00
|
|
|
|
{
|
2020-11-03 10:42:41 +01:00
|
|
|
|
var lambda = GenerateLambdaWithSafeMemberAccess<TEntity>(fullPropertyName, propertyInfo);
|
2020-10-17 20:10:52 +02:00
|
|
|
|
|
|
|
|
|
var command = desc
|
|
|
|
|
? (useThenBy ? "ThenByDescending" : "OrderByDescending")
|
|
|
|
|
: (useThenBy ? "ThenBy" : "OrderBy");
|
|
|
|
|
|
|
|
|
|
var resultExpression = Expression.Call(
|
|
|
|
|
typeof(Queryable),
|
|
|
|
|
command,
|
|
|
|
|
new Type[] { typeof(TEntity), lambda.ReturnType },
|
|
|
|
|
source.Expression,
|
|
|
|
|
Expression.Quote(lambda));
|
|
|
|
|
|
|
|
|
|
return source.Provider.CreateQuery<TEntity>(resultExpression);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 10:42:41 +01:00
|
|
|
|
private static Expression<Func<TEntity, object>> GenerateLambdaWithSafeMemberAccess<TEntity>
|
|
|
|
|
(
|
|
|
|
|
string fullPropertyName,
|
|
|
|
|
PropertyInfo propertyInfo
|
|
|
|
|
)
|
2020-10-17 20:10:52 +02:00
|
|
|
|
{
|
|
|
|
|
var parameter = Expression.Parameter(typeof(TEntity), "e");
|
|
|
|
|
Expression propertyValue = parameter;
|
|
|
|
|
Expression nullCheck = null;
|
|
|
|
|
|
|
|
|
|
foreach (var name in fullPropertyName.Split('.'))
|
2019-01-18 07:27:52 +01:00
|
|
|
|
{
|
2020-11-03 10:42:41 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
propertyValue = Expression.PropertyOrField(propertyValue, name);
|
|
|
|
|
}
|
|
|
|
|
catch (ArgumentException)
|
|
|
|
|
{
|
|
|
|
|
// name is not a direct property of field of propertyValue expression. construct a memberAccess then.
|
|
|
|
|
propertyValue = Expression.MakeMemberAccess(propertyValue, propertyInfo);
|
|
|
|
|
}
|
2020-10-17 20:10:52 +02:00
|
|
|
|
|
|
|
|
|
if (propertyValue.Type.IsNullable())
|
2019-01-18 07:27:52 +01:00
|
|
|
|
{
|
2020-10-17 20:10:52 +02:00
|
|
|
|
nullCheck = GenerateOrderNullCheckExpression(propertyValue, nullCheck);
|
2019-01-18 07:27:52 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-16 23:51:13 +01:00
|
|
|
|
|
2020-10-17 20:10:52 +02:00
|
|
|
|
var expression = nullCheck == null
|
|
|
|
|
? propertyValue
|
|
|
|
|
: Expression.Condition(nullCheck, Expression.Default(propertyValue.Type), propertyValue);
|
|
|
|
|
|
|
|
|
|
var converted = Expression.Convert(expression, typeof(object));
|
|
|
|
|
return Expression.Lambda<Func<TEntity, object>>(converted, parameter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Expression GenerateOrderNullCheckExpression(Expression propertyValue, Expression nullCheckExpression)
|
|
|
|
|
{
|
|
|
|
|
return nullCheckExpression == null
|
|
|
|
|
? Expression.Equal(propertyValue, Expression.Default(propertyValue.Type))
|
|
|
|
|
: Expression.OrElse(nullCheckExpression, Expression.Equal(propertyValue, Expression.Default(propertyValue.Type)));
|
2018-01-27 06:20:57 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|