@@ -227,12 +227,24 @@ private static Expression CreateRightExpr(Expression leftExpr, string right, Com
227227 {
228228 var targetType = leftExpr . Type ;
229229
230- // Check if this property uses HasConversion and should use the conversion target type
230+ // Check if this property uses HasConversion
231231 if ( config ? . PropertyMappings != null && ! string . IsNullOrEmpty ( propertyPath ) )
232232 {
233233 var propertyConfig = config . PropertyMappings . GetPropertyInfoByQueryName ( propertyPath ) ;
234234 if ( propertyConfig ? . UsesConversion == true && propertyConfig . ConversionTargetType != null )
235235 {
236+ // For HasConversion properties, try to create a constant of the original type
237+ // by constructing it from the string value using a constructor that takes the target type
238+ if ( propertyConfig . ConversionTargetType == typeof ( string ) )
239+ {
240+ var stringCtor = leftExpr . Type . GetConstructor ( new [ ] { typeof ( string ) } ) ;
241+ if ( stringCtor != null )
242+ {
243+ return Expression . New ( stringCtor , Expression . Constant ( right , typeof ( string ) ) ) ;
244+ }
245+ }
246+
247+ // For other conversion types, fall back to using the conversion target type
236248 targetType = propertyConfig . ConversionTargetType ;
237249 }
238250 }
@@ -613,23 +625,6 @@ private static Parser<Expression> ComparisonExprParser<T>(ParameterExpression pa
613625 return CreateNestedCollectionFilterExpression < T > ( methodCall , rightExpr , temp . op ) ;
614626 }
615627
616- // Special handling for HasConversion properties
617- if ( config ? . PropertyMappings != null && ! string . IsNullOrEmpty ( propertyPath ) )
618- {
619- var propertyConfig = config . PropertyMappings . GetPropertyInfoByQueryName ( propertyPath ) ;
620- if ( propertyConfig ? . UsesConversion == true && temp . op . Operator ( ) == "==" )
621- {
622- // For HasConversion properties, use Object.Equals instead of Expression.Equal
623- // This avoids the type compatibility check and lets EF Core handle the conversion
624- var equalsMethod = typeof ( object ) . GetMethod ( "Equals" , new [ ] { typeof ( object ) , typeof ( object ) } ) ;
625- if ( equalsMethod != null )
626- {
627- var leftAsObject = Expression . Convert ( temp . leftExpr , typeof ( object ) ) ;
628- var rightAsObject = Expression . Convert ( rightExpr , typeof ( object ) ) ;
629- return Expression . Call ( equalsMethod , leftAsObject , rightAsObject ) ;
630- }
631- }
632- }
633628
634629 return temp . op . GetExpression < T > ( temp . leftExpr , rightExpr , config ? . DbContextType ) ;
635630 } ) ;
0 commit comments