Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query: Parameterize components of NewExpression when possible #20522

Merged
merged 1 commit into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/EFCore/Query/Internal/ParameterExtractingExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,13 @@ protected override Expression VisitMemberInit(MemberInitExpression memberInitExp
Visit(memberInitExpression.Bindings, VisitMemberBinding);

// Cannot make parameter for NewExpression if Bindings cannot be evaluated
if (_evaluatable)
// but we still need to visit inside of it.
var bindingsEvaluatable = _evaluatable;
Visit(memberInitExpression.NewExpression);

if (!bindingsEvaluatable)
{
Visit(memberInitExpression.NewExpression);
_evaluatableExpressions.Remove(memberInitExpression.NewExpression);
}

return memberInitExpression;
Expand All @@ -542,9 +546,13 @@ protected override Expression VisitListInit(ListInitExpression listInitExpressio
Visit(listInitExpression.Initializers, VisitElementInit);

// Cannot make parameter for NewExpression if Initializers cannot be evaluated
if (_evaluatable)
// but we still need to visit inside of it.
var initializersEvaluatable = _evaluatable;
Visit(listInitExpression.NewExpression);

if (!initializersEvaluatable)
{
Visit(listInitExpression.NewExpression);
_evaluatableExpressions.Remove(listInitExpression.NewExpression);
}

return listInitExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5813,5 +5813,34 @@ public virtual Task Entity_equality_contains_with_list_of_null(bool async)
ss => ss.Set<Customer>().Where(c => customers.Contains(c)),
entryCount: 1);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task MemberInitExpression_NewExpression_is_funcletized_even_when_bindings_are_not_evaluatable(bool async)
{
var randomString = "random";
return AssertQuery(
async,
ss => ss.Set<Customer>().Where(c => c.CustomerID.StartsWith("A"))
.Select(c => new Dto(randomString)
{
CustomerID = c.CustomerID,
NestedDto = new Dto(randomString)
}),
elementSorter: e => e.CustomerID,
elementAsserter: (e, a) => Assert.Equal(e.CustomerID, a.CustomerID));
}

private class Dto
{
public Dto(string value)
{
Value = value;
}

public string Value { get; }
public string CustomerID { get; set; }
public Dto NestedDto { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5047,6 +5047,16 @@ FROM [Customers] AS [c]
WHERE [c].[CustomerID] IN (N'ALFKI')");
}

public override async Task MemberInitExpression_NewExpression_is_funcletized_even_when_bindings_are_not_evaluatable(bool async)
{
await base.MemberInitExpression_NewExpression_is_funcletized_even_when_bindings_are_not_evaluatable(async);

AssertSql(
@"SELECT [c].[CustomerID]
FROM [Customers] AS [c]
WHERE [c].[CustomerID] LIKE N'A%'");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

Expand Down