@@ -68,20 +68,20 @@ public static Expression GenerateSubtract(Expression left, Expression right)
68
68
public static Expression GenerateEqual ( Expression left , Expression right )
69
69
{
70
70
OptimizeForEqualityIfPossible ( ref left , ref right ) ;
71
-
72
- WrapConstantExpression ( ref left ) ;
73
- WrapConstantExpression ( ref right ) ;
74
-
71
+
72
+ ConstantExpressionWrapper . Wrap ( ref left ) ;
73
+ ConstantExpressionWrapper . Wrap ( ref right ) ;
74
+
75
75
return Expression . Equal ( left , right ) ;
76
76
}
77
77
78
78
public static Expression GenerateNotEqual ( Expression left , Expression right )
79
79
{
80
80
OptimizeForEqualityIfPossible ( ref left , ref right ) ;
81
-
82
- WrapConstantExpression ( ref left ) ;
83
- WrapConstantExpression ( ref right ) ;
84
-
81
+
82
+ ConstantExpressionWrapper . Wrap ( ref left ) ;
83
+ ConstantExpressionWrapper . Wrap ( ref right ) ;
84
+
85
85
return Expression . NotEqual ( left , right ) ;
86
86
}
87
87
@@ -98,9 +98,9 @@ public static Expression GenerateGreaterThan(Expression left, Expression right)
98
98
var rightPart = right . Type . GetTypeInfo ( ) . IsEnum ? Expression . Convert ( right , Enum . GetUnderlyingType ( right . Type ) ) : right ;
99
99
return Expression . GreaterThan ( leftPart , rightPart ) ;
100
100
}
101
-
102
- WrapConstantExpression ( ref left ) ;
103
- WrapConstantExpression ( ref right ) ;
101
+
102
+ ConstantExpressionWrapper . Wrap ( ref left ) ;
103
+ ConstantExpressionWrapper . Wrap ( ref right ) ;
104
104
105
105
return Expression . GreaterThan ( left , right ) ;
106
106
}
@@ -117,9 +117,9 @@ public static Expression GenerateGreaterThanEqual(Expression left, Expression ri
117
117
return Expression . GreaterThanOrEqual ( left . Type . GetTypeInfo ( ) . IsEnum ? Expression . Convert ( left , Enum . GetUnderlyingType ( left . Type ) ) : left ,
118
118
right . Type . GetTypeInfo ( ) . IsEnum ? Expression . Convert ( right , Enum . GetUnderlyingType ( right . Type ) ) : right ) ;
119
119
}
120
-
121
- WrapConstantExpression ( ref left ) ;
122
- WrapConstantExpression ( ref right ) ;
120
+
121
+ ConstantExpressionWrapper . Wrap ( ref left ) ;
122
+ ConstantExpressionWrapper . Wrap ( ref right ) ;
123
123
124
124
return Expression . GreaterThanOrEqual ( left , right ) ;
125
125
}
@@ -137,8 +137,8 @@ public static Expression GenerateLessThan(Expression left, Expression right)
137
137
right . Type . GetTypeInfo ( ) . IsEnum ? Expression . Convert ( right , Enum . GetUnderlyingType ( right . Type ) ) : right ) ;
138
138
}
139
139
140
- WrapConstantExpression ( ref left ) ;
141
- WrapConstantExpression ( ref right ) ;
140
+ ConstantExpressionWrapper . Wrap ( ref left ) ;
141
+ ConstantExpressionWrapper . Wrap ( ref right ) ;
142
142
143
143
return Expression . LessThan ( left , right ) ;
144
144
}
@@ -156,8 +156,8 @@ public static Expression GenerateLessThanEqual(Expression left, Expression right
156
156
right . Type . GetTypeInfo ( ) . IsEnum ? Expression . Convert ( right , Enum . GetUnderlyingType ( right . Type ) ) : right ) ;
157
157
}
158
158
159
- WrapConstantExpression ( ref left ) ;
160
- WrapConstantExpression ( ref right ) ;
159
+ ConstantExpressionWrapper . Wrap ( ref left ) ;
160
+ ConstantExpressionWrapper . Wrap ( ref right ) ;
161
161
162
162
return Expression . LessThanOrEqual ( left , right ) ;
163
163
}
@@ -208,10 +208,10 @@ public static Expression OptimizeStringForEqualityIfPossible(string text, Type t
208
208
209
209
static MethodInfo GetStaticMethod ( string methodName , Expression left , Expression right )
210
210
{
211
- var methodInfo = left . Type . GetMethod ( methodName , new [ ] { left . Type , right . Type } ) ;
211
+ var methodInfo = left . Type . GetMethod ( methodName , new [ ] { left . Type , right . Type } ) ;
212
212
if ( methodInfo == null )
213
213
{
214
- methodInfo = right . Type . GetMethod ( methodName , new [ ] { left . Type , right . Type } ) ;
214
+ methodInfo = right . Type . GetMethod ( methodName , new [ ] { left . Type , right . Type } ) ;
215
215
}
216
216
217
217
return methodInfo ;
@@ -221,65 +221,5 @@ static Expression GenerateStaticMethodCall(string methodName, Expression left, E
221
221
{
222
222
return Expression . Call ( null , GetStaticMethod ( methodName , left , right ) , new [ ] { left , right } ) ;
223
223
}
224
-
225
- static void WrapConstantExpression ( ref Expression expression )
226
- {
227
- if ( ! ( expression is ConstantExpression || expression is NewExpression ) ) return ;
228
-
229
- if ( expression is ConstantExpression )
230
- {
231
- ConstantExpression constantExpression = expression as ConstantExpression ;
232
-
233
- if ( constantExpression . Type == typeof ( string ) )
234
- {
235
- expression = WrappedConstant ( ( string ) constantExpression . Value ) ;
236
- }
237
- else if ( constantExpression . Type == typeof ( long ) )
238
- {
239
- expression = WrappedConstant ( ( Int64 ) constantExpression . Value ) ;
240
- }
241
- else if ( constantExpression . Type == typeof ( int ) )
242
- {
243
- expression = WrappedConstant ( ( Int32 ) constantExpression . Value ) ;
244
- }
245
- else if ( constantExpression . Type == typeof ( short ) )
246
- {
247
- expression = WrappedConstant ( ( Int16 ) constantExpression . Value ) ;
248
- }
249
- else if ( constantExpression . Type == typeof ( Guid ) )
250
- {
251
- expression = WrappedConstant ( ( Guid ) constantExpression . Value ) ;
252
- }
253
- else if ( constantExpression . Type == typeof ( DateTime ) )
254
- {
255
- expression = WrappedConstant ( ( DateTime ) constantExpression . Value ) ;
256
- }
257
- }
258
- else
259
- {
260
- NewExpression newExpression = expression as NewExpression ;
261
-
262
- if ( newExpression . Type == typeof ( Guid ) )
263
- {
264
- expression = WrappedConstant ( ( Expression . Lambda < Func < Guid > > ( newExpression ) . Compile ( ) ) ( ) ) ;
265
- }
266
- else if ( newExpression . Type == typeof ( DateTime ) )
267
- {
268
- expression = WrappedConstant ( ( Expression . Lambda < Func < DateTime > > ( newExpression ) . Compile ( ) ) ( ) ) ;
269
- }
270
- }
271
- }
272
-
273
- /// <summary>
274
- /// Based on gblog by graeme-hill. https://github.com/graeme-hill/gblog/blob/master/source_content/articles/2014.139_entity-framework-dynamic-queries-and-parameterization.mkd
275
- /// </summary>
276
- private static MemberExpression WrappedConstant < TValue > ( TValue value )
277
- {
278
- var wrapper = new WrappedObj < TValue > ( value ) ;
279
- return Expression . Property (
280
- Expression . Constant ( wrapper ) ,
281
- typeof ( WrappedObj < TValue > ) . GetProperty ( "Value" ) ) ;
282
- }
283
-
284
224
}
285
225
}
0 commit comments