-
Notifications
You must be signed in to change notification settings - Fork 127
/
OrderByDynamic.cs
76 lines (66 loc) · 2.82 KB
/
OrderByDynamic.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace Sieve.Extensions
{
public static partial class LinqExtensions
{
public static IQueryable<TEntity> OrderByDynamic<TEntity>(
this IQueryable<TEntity> source,
string fullPropertyName,
PropertyInfo propertyInfo,
bool desc,
bool useThenBy)
{
var lambda = GenerateLambdaWithSafeMemberAccess<TEntity>(fullPropertyName, propertyInfo);
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);
}
private static Expression<Func<TEntity, object>> GenerateLambdaWithSafeMemberAccess<TEntity>
(
string fullPropertyName,
PropertyInfo propertyInfo
)
{
var parameter = Expression.Parameter(typeof(TEntity), "e");
Expression propertyValue = parameter;
Expression nullCheck = null;
foreach (var name in fullPropertyName.Split('.'))
{
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);
}
if (propertyValue.Type.IsNullable())
{
nullCheck = GenerateOrderNullCheckExpression(propertyValue, nullCheck);
}
}
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)));
}
}
}