Skip to content

Commit

Permalink
Query: Don't use VisitAndConvert in projectionBindingExpressionVisitor (
Browse files Browse the repository at this point in the history
#18975)

Since visiting inner could return null

Resolves #18888
  • Loading branch information
smitpatel committed Dec 10, 2019
1 parent b0da3eb commit 813358a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ protected override Expression VisitNew(NewExpression newExpression)
/// </summary>
protected override Expression VisitMemberInit(MemberInitExpression memberInitExpression)
{
var newExpression = VisitAndConvert(memberInitExpression.NewExpression, nameof(VisitMemberInit));
var newExpression = Visit(memberInitExpression.NewExpression);
if (newExpression == null)
{
return null;
Expand All @@ -530,7 +530,7 @@ protected override Expression VisitMemberInit(MemberInitExpression memberInitExp
}
}

return memberInitExpression.Update(newExpression, newBindings);
return memberInitExpression.Update((NewExpression)newExpression, newBindings);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ protected override Expression VisitNew(NewExpression newExpression)

protected override Expression VisitMemberInit(MemberInitExpression memberInitExpression)
{
var newExpression = VisitAndConvert(memberInitExpression.NewExpression, nameof(VisitMemberInit));
var newExpression = Visit(memberInitExpression.NewExpression);
if (newExpression == null)
{
return null;
Expand All @@ -288,7 +288,7 @@ protected override Expression VisitMemberInit(MemberInitExpression memberInitExp
}
}

return memberInitExpression.Update(newExpression, newBindings);
return memberInitExpression.Update((NewExpression)newExpression, newBindings);
}

protected override MemberAssignment VisitMemberAssignment(MemberAssignment memberAssignment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ protected override Expression VisitNew(NewExpression newExpression)

protected override Expression VisitMemberInit(MemberInitExpression memberInitExpression)
{
var newExpression = VisitAndConvert(memberInitExpression.NewExpression, nameof(VisitMemberInit));
var newExpression = Visit(memberInitExpression.NewExpression);
if (newExpression == null)
{
return null;
Expand All @@ -290,7 +290,7 @@ protected override Expression VisitMemberInit(MemberInitExpression memberInitExp
}
}

return memberInitExpression.Update(newExpression, newBindings);
return memberInitExpression.Update((NewExpression)newExpression, newBindings);
}

protected override MemberAssignment VisitMemberAssignment(MemberAssignment memberAssignment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1457,5 +1457,40 @@ public virtual Task LastOrDefault_member_access_in_projection_translates_to_serv
.Select(c => new { c, c.Orders.OrderByDescending(o => o.OrderID).LastOrDefault().OrderDate }),
entryCount: 4);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Projection_with_parameterized_constructor(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Customer>().Where(c => c.CustomerID == "ALFKI").Select(c => new CustomerWrapper2(c)),
entryCount: 1,
elementSorter: e => e.Customer.CustomerID,
elementAsserter: (e, a) => Assert.Equal(e.Customer, a.Customer));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Projection_with_parameterized_constructor_with_member_assignment(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Customer>().Where(c => c.CustomerID == "ALFKI").Select(c => new CustomerWrapper2(c) { City = c.City }),
entryCount: 1,
elementSorter: e => e.Customer.CustomerID,
elementAsserter: (e, a) => Assert.Equal(e.Customer, a.Customer));
}

private class CustomerWrapper2
{
public CustomerWrapper2(Customer customer)
{
Customer = customer;
}

public string City { get; set; }
public Customer Customer { get; }
}
}
}

0 comments on commit 813358a

Please sign in to comment.