Skip to content

Commit a99ed96

Browse files
committed
fix: hasconversion
1 parent ccbc3af commit a99ed96

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

QueryKit.IntegrationTests/Tests/DatabaseFilteringTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace QueryKit.IntegrationTests.Tests;
1717
using WebApiTestProject.Entities.Recipes;
1818
using Xunit.Abstractions;
1919

20-
public class DatabaseFilteringTests(ITestOutputHelper testOutputHelper) : TestBase
20+
public class DatabaseFilteringTests() : TestBase
2121
{
2222
[Fact]
2323
public async Task can_filter_by_string()

QueryKit/FilterParser.cs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)