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

Entity Equality does not rewrite Contains on Array #19433

Closed
smitpatel opened this issue Dec 30, 2019 · 2 comments · Fixed by #20489
Closed

Entity Equality does not rewrite Contains on Array #19433

smitpatel opened this issue Dec 30, 2019 · 2 comments · Fixed by #20489
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@smitpatel
Copy link
Contributor

        [ConditionalTheory]
        [MemberData(nameof(IsAsyncData))]
        public virtual Task Where_collection_navigation_ToArray_Contains(bool async)
        {
            using var context = CreateContext();
            var order = context.Set<Order>().FirstOrDefault(o => o.OrderID == 10248);

            return AssertQuery(
                async,
                ss => ss.Set<Customer>()
                    .Select(c => c.Orders.ToArray())
                    .Where(e => e.Contains(order)),
                entryCount: 5);
        }

Fails with

Message: 
    System.InvalidOperationException : The LINQ expression 'DbSet<Order>
        .Where(o => EF.Property<string>((EntityShaperExpression: 
            EntityType: Customer
            ValueBufferExpression: 
                (ProjectionBindingExpression: EmptyProjectionMember)
            IsNullable: False
        ), "CustomerID") != null && EF.Property<string>((EntityShaperExpression: 
            EntityType: Customer
            ValueBufferExpression: 
                (ProjectionBindingExpression: EmptyProjectionMember)
            IsNullable: False
        ), "CustomerID") == EF.Property<string>(o, "CustomerID"))
        .Contains(__order_0)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
  Stack Trace: 
    QueryableMethodTranslatingExpressionVisitor.<VisitMethodCall>g__CheckTranslated|8_0(ShapedQueryExpression translated, <>c__DisplayClass8_0& ) line 59
    QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) line 123
    RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) line 78
    MethodCallExpression.Accept(ExpressionVisitor visitor)
    ExpressionVisitor.Visit(Expression node)
    QueryableMethodTranslatingExpressionVisitor.TranslateSubquery(Expression expression) line 560
    RelationalSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) line 365
    MethodCallExpression.Accept(ExpressionVisitor visitor)
    ExpressionVisitor.Visit(Expression node)
    RelationalSqlTranslatingExpressionVisitor.Translate(Expression expression) line 50
    RelationalQueryableMethodTranslatingExpressionVisitor.TranslateExpression(Expression expression) line 1045
    RelationalQueryableMethodTranslatingExpressionVisitor.TranslateLambdaExpression(ShapedQueryExpression shapedQueryExpression, LambdaExpression lambdaExpression) line 1049
    RelationalQueryableMethodTranslatingExpressionVisitor.TranslateWhere(ShapedQueryExpression source, LambdaExpression predicate) line 1034
    QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) line 435
    RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) line 78
    MethodCallExpression.Accept(ExpressionVisitor visitor)
    ExpressionVisitor.Visit(Expression node)
    QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) line 66
    RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) line 78
    MethodCallExpression.Accept(ExpressionVisitor visitor)
    ExpressionVisitor.Visit(Expression node)
    QueryCompilationContext.CreateQueryExecutor[TResult](Expression query) line 76
    Database.CompileQuery[TResult](Expression query, Boolean async) line 71
    QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async) line 112
    <>c__DisplayClass9_0`1.<Execute>b__0() line 96
    CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler) line 84
    CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler) line 59
    QueryCompiler.Execute[TResult](Expression query) line 92
    EntityQueryProvider.Execute[TResult](Expression expression) line 79
    EntityQueryable`1.GetEnumerator() line 95
    LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
    EnumerableHelpers.ToArray[T](IEnumerable`1 source)
    Enumerable.ToArray[TSource](IEnumerable`1 source)
    QueryAsserter`1.AssertQuery[TResult](Func`2 actualQuery, Func`2 expectedQuery, Func`2 elementSorter, Action`2 elementAsserter, Boolean assertOrder, Int32 entryCount, Boolean async, String testMethodName) line 91
    --- End of stack trace from previous location where exception was thrown ---

Same with ToList/AsEnumerable passes.

@smitpatel
Copy link
Contributor Author

Related #20164

@smitpatel
Copy link
Contributor Author

This gets converted to

Message: 
    System.InvalidOperationException : The LINQ expression 'DbSet<Customer>()
        .Where(c => MaterializeCollectionNavigation(
            navigation: Navigation: Customer.Orders,
            subquery: DbSet<Order>()
                .Where(o => EF.Property<string>(c, "CustomerID") != null && EF.Property<string>(c, "CustomerID") == EF.Property<string>(o, "CustomerID")).ToArray()
            .Any(p => p == __order_0))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

Blocked by #20155

@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 1, 2020
smitpatel added a commit that referenced this issue Apr 1, 2020
Part of #18923

Resolves #20155
Resolves #20369
We convert Queryable.Contains to Queryable.Any after navigation expansion has run so only true queraybles would have Queryable.Contains. Array properties would have Enumerable.Contains hence does not get rewritten.

Resolves #19433
smitpatel added a commit that referenced this issue Apr 1, 2020
Part of #18923

Resolves #20155
Resolves #20369
We convert Queryable.Contains to Queryable.Any after navigation expansion has run so only true queraybles would have Queryable.Contains. Array properties would have Enumerable.Contains hence does not get rewritten.

Resolves #19433
smitpatel added a commit that referenced this issue Apr 1, 2020
Part of #18923

Resolves #20155
Resolves #20369
We convert Queryable.Contains to Queryable.Any after navigation expansion has run so only true queraybles would have Queryable.Contains. Array properties would have Enumerable.Contains hence does not get rewritten.

Resolves #19433
@ajcvickers ajcvickers modified the milestones: 5.0.0, 5.0.0-preview4 Apr 20, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-preview4, 5.0.0 Nov 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants