Use right hand type mapping for more binary expressions when left is an object #34729
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
With a query like
Select(x => x != null ? x.Id + "" : null).FirstOrDefault(x => x!.Contains("1"));
you have a binary expression ofx.Id + ""
. The left hand side comes through from LINQ without any type mapping and when inferring the type mapping for the result of the binary expression it always looks at the left hand side so you end up with a Type ofobject
and a null type mapping for the expressionWith the order of the
Select
it ends up trying to take the type mapping ofx.Id + ""
for the result of theCASE
. When you then do aStartsWith
,EndsWith
.Contains
all those are translated as aLIKE
and no type mapping is changed.It works with the equals
Select(x => x != null ? x.Id + "" : null).FirstOrDefault(x => x == "1");
as that code does look at the right hand side if the left is an object.See line 185 in SqlExpressionFactory
When you flip the Select around (
Select(x => x == null ? null : x.Id + "").FirstOrDefault(x => x!.Contains("1"));
) it works because thenull
result with type mapping of string is used for the result of theCASE
and the type mapping is applied on all expressionsThis change adds the same type of logic elsewhere in the
ApplyTypeMappingOnSqlBinary
for the Add, Subtract etc operationsTests added in AdHocMiscellaneousQueryTestBase.
Fixes #34618