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

Solve the 3.0 regression: Cannot use generics w/ interface to construct expression bug #18651

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,16 @@ private static T GetParameterValue<T>(QueryContext queryContext, string paramete

protected override Expression VisitUnary(UnaryExpression unaryExpression)
{

// bypass the inner conversion if it is an interface
if (unaryExpression.Operand is UnaryExpression operandUnary
&& operandUnary.NodeType == ExpressionType.Convert
&& operandUnary.Type.IsInterface)
{
// update the unaryExpression to use the inner expression
return Visit(unaryExpression.Update(operandUnary.Operand));
}

var newOperand = Visit(unaryExpression.Operand);
if (newOperand == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,15 @@ protected override Expression VisitConditional(ConditionalExpression conditional

protected override Expression VisitUnary(UnaryExpression unaryExpression)
{
// bypass the inner conversion if it is an interface
if (unaryExpression.Operand is UnaryExpression operandUnary
&& operandUnary.NodeType == ExpressionType.Convert
&& operandUnary.Type.IsInterface)
{
// update the unaryExpression to use the inner expression
return Visit(unaryExpression.Update(operandUnary.Operand));
}

var operand = Visit(unaryExpression.Operand);

if (operand is EntityProjectionExpression)
Expand Down
17 changes: 17 additions & 0 deletions test/EFCore.Specification.Tests/Query/SimpleQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5165,6 +5165,23 @@ public virtual Task OrderBy_Dto_projection_skip_take(bool isAsync)
elementSorter: e => e.Id);
}

static Expression<Func<T, bool>> HasOrderSince<T>(DateTime since)
where T : IOrders
{
Expression<Func<T, bool>> predicate = oa => oa.Orders.AsQueryable().Any(o=> o.OrderDate > since);
return predicate;
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Explicit_cast_to_Interface_bug_17794(bool isAsync)
{
var expr = HasOrderSince<Customer>(new DateTime(2019,10,30));
return AssertQuery(
isAsync,
ss => ss.Set<Customer>().Where(expr).Select(c => c.CustomerID));
}

[ConditionalFact(Skip = "Issue #16314")]
public virtual void Streaming_chained_sync_query()
{
Expand Down
11 changes: 10 additions & 1 deletion test/EFCore.Specification.Tests/TestModels/Northwind/Customer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@

namespace Microsoft.EntityFrameworkCore.TestModels.Northwind
{
public class Customer : IComparable<Customer>
public interface ICustomerID
{
string CustomerID { get; }
}

public interface IOrders
{
List<Order> Orders { get; }
}
public class Customer : IComparable<Customer>, ICustomerID, IOrders
{
public Customer()
{
Expand Down