-
Notifications
You must be signed in to change notification settings - Fork 467
Expand file tree
/
Copy pathExpressionHelpers.cs
More file actions
296 lines (261 loc) · 13 KB
/
ExpressionHelpers.cs
File metadata and controls
296 lines (261 loc) · 13 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.AspNet.OData.Formatter;
using Microsoft.AspNet.OData.Query.Expressions;
using Microsoft.OData.Edm;
using Microsoft.OData.UriParser;
namespace Microsoft.AspNet.OData
{
internal static class ExpressionHelpers
{
public static Func<long> Count(IQueryable query, Type type)
{
MethodInfo countMethod = ExpressionHelperMethods.QueryableCountGeneric.MakeGenericMethod(type);
Func<long> func = () => (long)countMethod.Invoke(null, new object[] { query });
return func;
}
public static IQueryable Skip(IQueryable query, int count, Type type, bool parameterize)
{
MethodInfo skipMethod = ExpressionHelperMethods.QueryableSkipGeneric.MakeGenericMethod(type);
Expression skipValueExpression = parameterize ? LinqParameterContainer.Parameterize(typeof(int), count) : Expression.Constant(count);
Expression skipQuery = Expression.Call(null, skipMethod, new[] { query.Expression, skipValueExpression });
var createMethod = ExpressionHelperMethods.CreateQueryGeneric.MakeGenericMethod(type);
return createMethod.Invoke(query.Provider, new[] { skipQuery }) as IQueryable;
}
public static IQueryable Take(IQueryable query, int count, Type type, bool parameterize)
{
Expression takeQuery = Take(query.Expression, count, type, parameterize);
var createMethod = ExpressionHelperMethods.CreateQueryGeneric.MakeGenericMethod(type);
return createMethod.Invoke(query.Provider, new[] { takeQuery }) as IQueryable;
}
public static Expression Skip(Expression source, int count, Type type, bool parameterize)
{
MethodInfo skipMethod;
if (typeof(IQueryable).IsAssignableFrom(source.Type))
{
skipMethod = ExpressionHelperMethods.QueryableSkipGeneric.MakeGenericMethod(type);
}
else
{
skipMethod = ExpressionHelperMethods.EnumerableSkipGeneric.MakeGenericMethod(type);
}
Expression skipValueExpression = parameterize ? LinqParameterContainer.Parameterize(typeof(int), count) : Expression.Constant(count);
Expression skipQuery = Expression.Call(null, skipMethod, new[] { source, skipValueExpression });
return skipQuery;
}
public static Expression Take(Expression source, int count, Type elementType, bool parameterize)
{
MethodInfo takeMethod;
if (typeof(IQueryable).IsAssignableFrom(source.Type))
{
takeMethod = ExpressionHelperMethods.QueryableTakeGeneric.MakeGenericMethod(elementType);
}
else
{
takeMethod = ExpressionHelperMethods.EnumerableTakeGeneric.MakeGenericMethod(elementType);
}
Expression takeValueExpression = parameterize ? LinqParameterContainer.Parameterize(typeof(int), count) : Expression.Constant(count);
Expression takeQuery = Expression.Call(null, takeMethod, new[] { source, takeValueExpression });
return takeQuery;
}
public static Expression Count(Expression source, Type elementType)
{
MethodInfo countMethod;
if (typeof(IQueryable).IsAssignableFrom(source.Type))
{
countMethod = ExpressionHelperMethods.QueryableCountGeneric.MakeGenericMethod(elementType);
}
else
{
countMethod = ExpressionHelperMethods.EnumerableCountGeneric.MakeGenericMethod(elementType);
}
Expression countExpression = Expression.Call(null, countMethod, new[] { source });
return countExpression;
}
public static Expression OrderByPropertyExpression(
Expression source,
string propertyName,
Type elementType,
bool alreadyOrdered = false)
{
LambdaExpression orderByLambda = GetPropertyAccessLambda(elementType, propertyName);
return OrderBy(source, orderByLambda, elementType, OrderByDirection.Ascending, alreadyOrdered);
}
public static Expression OrderBy(
Expression source,
LambdaExpression orderByLambda,
Type elementType,
OrderByDirection direction,
bool alreadyOrdered = false)
{
Type returnType = orderByLambda.Body.Type;
MethodInfo orderByMethod;
if (!alreadyOrdered)
{
if (typeof(IQueryable).IsAssignableFrom(source.Type))
{
if (direction == OrderByDirection.Ascending)
{
orderByMethod = ExpressionHelperMethods.QueryableOrderByGeneric.MakeGenericMethod(elementType,
returnType);
}
else
{
orderByMethod = ExpressionHelperMethods.QueryableOrderByDescendingGeneric.MakeGenericMethod(elementType,
returnType);
}
}
else
{
if (direction == OrderByDirection.Ascending)
{
orderByMethod = ExpressionHelperMethods.EnumerableOrderByGeneric.MakeGenericMethod(elementType,
returnType);
}
else
{
orderByMethod = ExpressionHelperMethods.EnumerableOrderByDescendingGeneric.MakeGenericMethod(elementType,
returnType);
}
}
}
else
{
if (typeof(IQueryable).IsAssignableFrom(source.Type))
{
if (direction == OrderByDirection.Ascending)
{
orderByMethod = ExpressionHelperMethods.QueryableThenByGeneric.MakeGenericMethod(elementType,
returnType);
}
else
{
orderByMethod = ExpressionHelperMethods.QueryableThenByDescendingGeneric.MakeGenericMethod(elementType,
returnType);
}
}
else
{
if (direction == OrderByDirection.Ascending)
{
orderByMethod = ExpressionHelperMethods.EnumerableThenByGeneric.MakeGenericMethod(elementType,
returnType);
}
else
{
orderByMethod = ExpressionHelperMethods.EnumerableThenByDescendingGeneric.MakeGenericMethod(elementType,
returnType);
}
}
}
return Expression.Call(null, orderByMethod, new[] { source, orderByLambda });
}
public static IQueryable OrderByIt(IQueryable query, OrderByDirection direction, Type type, bool alreadyOrdered = false)
{
ParameterExpression odataItParameter = Expression.Parameter(type, "$it");
LambdaExpression orderByLambda = Expression.Lambda(odataItParameter, odataItParameter);
return OrderBy(query, orderByLambda, direction, type, alreadyOrdered);
}
public static IQueryable OrderByProperty(IQueryable query, IEdmModel model, IEdmProperty property, OrderByDirection direction, Type type, bool alreadyOrdered = false)
{
// property aliasing
string propertyName = EdmLibHelpers.GetClrPropertyName(property, model);
LambdaExpression orderByLambda = GetPropertyAccessLambda(type, propertyName);
return OrderBy(query, orderByLambda, direction, type, alreadyOrdered);
}
public static IQueryable OrderBy(IQueryable query, LambdaExpression orderByLambda, OrderByDirection direction, Type type, bool alreadyOrdered = false)
{
Type returnType = orderByLambda.Body.Type;
MethodInfo orderByMethod = null;
IOrderedQueryable orderedQuery = null;
// unfortunately unordered L2O.AsQueryable implements IOrderedQueryable
// so we can't try casting to IOrderedQueryable to provide a clue to whether
// we should be calling ThenBy or ThenByDescending
if (alreadyOrdered)
{
if (direction == OrderByDirection.Ascending)
{
orderByMethod = ExpressionHelperMethods.QueryableThenByGeneric.MakeGenericMethod(type, returnType);
}
else
{
orderByMethod = ExpressionHelperMethods.QueryableThenByDescendingGeneric.MakeGenericMethod(type, returnType);
}
orderedQuery = query as IOrderedQueryable;
orderedQuery = orderByMethod.Invoke(null, new object[] { orderedQuery, orderByLambda }) as IOrderedQueryable;
}
else
{
if (direction == OrderByDirection.Ascending)
{
orderByMethod = ExpressionHelperMethods.QueryableOrderByGeneric.MakeGenericMethod(type, returnType);
}
else
{
orderByMethod = ExpressionHelperMethods.QueryableOrderByDescendingGeneric.MakeGenericMethod(type, returnType);
}
orderedQuery = orderByMethod.Invoke(null, new object[] { query, orderByLambda }) as IOrderedQueryable;
}
return orderedQuery;
}
public static IQueryable GroupBy(IQueryable query, Expression expression, Type type, Type wrapperType)
{
MethodInfo groupByMethod = ExpressionHelperMethods.QueryableGroupByGeneric.MakeGenericMethod(type, wrapperType);
return groupByMethod.Invoke(null, new object[] { query, expression }) as IQueryable;
}
public static IQueryable Select(IQueryable query, LambdaExpression expression, Type type)
{
MethodInfo selectMethod = ExpressionHelperMethods.QueryableSelectGeneric.MakeGenericMethod(type, expression.Body.Type);
return selectMethod.Invoke(null, new object[] { query, expression }) as IQueryable;
}
public static IQueryable SelectMany(IQueryable query, LambdaExpression expression, Type type)
{
MethodInfo selectManyMethod = ExpressionHelperMethods.QueryableSelectManyGeneric.MakeGenericMethod(type, expression.Body.Type);
return selectManyMethod.Invoke(null, new object[] { query, expression }) as IQueryable;
}
public static IQueryable Aggregate(IQueryable query, object init, LambdaExpression sumLambda, Type type, Type wrapperType)
{
Type returnType = sumLambda.Body.Type;
MethodInfo sumMethod = ExpressionHelperMethods.QueryableAggregateGeneric.MakeGenericMethod(type, returnType);
var agg = sumMethod.Invoke(null, new object[] { query, init, sumLambda });
MethodInfo converterMethod = ExpressionHelperMethods.EntityAsQueryable.MakeGenericMethod(wrapperType);
return converterMethod.Invoke(null, new object[] { agg }) as IQueryable;
}
public static IQueryable Where(IQueryable query, Expression where, Type type)
{
MethodInfo whereMethod = ExpressionHelperMethods.QueryableWhereGeneric.MakeGenericMethod(type);
return whereMethod.Invoke(null, new object[] { query, where }) as IQueryable;
}
// If the expression is not a nullable type, cast it to one.
public static Expression ToNullable(Expression expression)
{
if (!TypeHelper.IsNullable(expression.Type))
{
return Expression.Convert(expression, TypeHelper.ToNullable(expression.Type));
}
return expression;
}
// Entity Framework does not understand default(T) expression. Hence, generate a constant expression with the default value.
public static Expression Default(Type type)
{
if (TypeHelper.IsValueType(type))
{
return Expression.Constant(Activator.CreateInstance(type), type);
}
else
{
return Expression.Constant(null, type);
}
}
public static LambdaExpression GetPropertyAccessLambda(Type type, string propertyName)
{
ParameterExpression odataItParameter = Expression.Parameter(type, "$it");
MemberExpression propertyAccess = Expression.Property(odataItParameter, propertyName);
return Expression.Lambda(propertyAccess, odataItParameter);
}
}
}