@@ -228,11 +228,8 @@ static Expression GetExpression(string path, Comparison comparison, string?[]? v
228228
229229 static MethodCallExpression MakeObjectListInComparision ( string [ ] values , Property < T > property )
230230 {
231- // Attempt to convert the string values to the object type
232231 var objects = TypeConverter . ConvertStringsToList ( values , property . Info ) ;
233- // Make the object values a constant expression
234- var constant = Expression . Constant ( objects ) ;
235- // Build and return the expression body
232+ var constant = MakeParameterizedConstant ( objects , objects . GetType ( ) ) ;
236233 return Expression . Call ( constant , property . SafeListContains , property . Left ) ;
237234 }
238235
@@ -244,14 +241,14 @@ static MethodCallExpression MakeStringListInComparison(string[] values, Property
244241 var itemEvaluate = Expression . Lambda < Func < string , bool > > ( equalsBody , ExpressionCache . StringParam ) ;
245242
246243 // Build Expression body to check if any string values match the property value
247- return Expression . Call ( null , ReflectionCache . StringAny , Expression . Constant ( values ) , itemEvaluate ) ;
244+ return Expression . Call ( null , ReflectionCache . StringAny , MakeParameterizedConstant ( values , typeof ( string [ ] ) ) , itemEvaluate ) ;
248245 }
249246
250247 static Expression MakeSingleStringComparison ( Comparison comparison , string ? value , Property < T > property )
251248 {
252249 var left = property . Left ;
253250
254- var valueConstant = Expression . Constant ( value , typeof ( string ) ) ;
251+ var valueConstant = MakeParameterizedConstant ( value , typeof ( string ) ) ;
255252 var nullCheck = Expression . NotEqual ( left , ExpressionCache . Null ) ;
256253
257254 switch ( comparison )
@@ -280,7 +277,7 @@ static Expression MakeSingleStringComparison(Comparison comparison, string? valu
280277 static Expression MakeSingleObjectComparison ( Comparison comparison , object ? value , Property < T > property )
281278 {
282279 var left = property . Left ;
283- var constant = Expression . Constant ( value , left . Type ) ;
280+ var constant = MakeParameterizedConstant ( value , left . Type ) ;
284281
285282 return comparison switch
286283 {
@@ -294,6 +291,15 @@ static Expression MakeSingleObjectComparison(Comparison comparison, object? valu
294291 } ;
295292 }
296293
294+ // Wraps a value in a field access on a holder object so that EF Core
295+ // emits a SQL parameter (@p0) instead of inlining the value as a literal.
296+ static Expression MakeParameterizedConstant ( object ? value , Type targetType )
297+ {
298+ var holder = new ValueHolder ( value ) ;
299+ Expression access = Expression . Field ( Expression . Constant ( holder ) , "Value" ) ;
300+ return Expression . Convert ( access , targetType ) ;
301+ }
302+
297303 static bool HasListPropertyInPath ( string path ) =>
298304 path . Contains ( '[' ) ;
299305
@@ -310,4 +316,9 @@ static Expression NegateExpression(Expression expression) =>
310316
311317 [ GeneratedRegex ( @"\[(.*)\]" ) ]
312318 private static partial Regex ListPropertyRegex ( ) ;
319+ }
320+
321+ class ValueHolder ( object ? value )
322+ {
323+ public object ? Value = value ;
313324}
0 commit comments