diff --git a/src/EFCore.SqlServer/Query/Internal/SearchConditionConverter.cs b/src/EFCore.SqlServer/Query/Internal/SearchConditionConverter.cs index 5e827602edc..0420834209a 100644 --- a/src/EFCore.SqlServer/Query/Internal/SearchConditionConverter.cs +++ b/src/EFCore.SqlServer/Query/Internal/SearchConditionConverter.cs @@ -206,9 +206,11 @@ protected virtual Expression VisitSqlBinary(SqlBinaryExpression binary, bool inS if (binary.OperatorType is ExpressionType.NotEqual or ExpressionType.Equal) { + var leftType = newLeft.TypeMapping?.Converter?.ProviderClrType ?? newLeft.Type; + var rightType = newRight.TypeMapping?.Converter?.ProviderClrType ?? newRight.Type; if (!inSearchConditionContext - && (newLeft.Type == typeof(bool) || newLeft.Type.IsEnum || newLeft.Type.IsInteger()) - && (newRight.Type == typeof(bool) || newRight.Type.IsEnum || newRight.Type.IsInteger())) + && (leftType == typeof(bool) || leftType.IsEnum || leftType.IsInteger()) + && (rightType == typeof(bool) || rightType.IsEnum || rightType.IsInteger())) { // "lhs != rhs" is the same as "CAST(lhs ^ rhs AS BIT)", except that // the first is a boolean, the second is a BIT @@ -265,7 +267,9 @@ protected virtual Expression VisitSqlUnary(SqlUnaryExpression sqlUnaryExpression case ExpressionType.Not when sqlUnaryExpression.Type == typeof(bool): { // when possible, avoid converting to/from predicate form - if (!inSearchConditionContext && sqlUnaryExpression.Operand is not (ExistsExpression or InExpression or LikeExpression)) + if (!inSearchConditionContext && + sqlUnaryExpression.TypeMapping?.Converter is null && + sqlUnaryExpression.Operand is not (ExistsExpression or InExpression or LikeExpression)) { var negatedOperand = (SqlExpression)Visit(sqlUnaryExpression.Operand);