You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
.Any() on collection navigations after projection can cause client-side evaluation #12728
In some cases using Where clauses of the form .Where(x => x.Collection.Any(...)) or .Where(x => x.Collection.Where(...).Any()) can cause client-side evaluation when the Where clause follows .Select. It seems that presence or absence of .ToList() inside .Select(...) makes a difference here when projecting to a DTO with an IEnumerable collection property.
Steps to reproduce
Here is a project which configures client-side evaluation as an exception, causing some Xunit test cases to fail.
A query like
dbContext.Set<Library>().Where(l =>l.Books.Any(b =>b.Title=="The Cat in the Hat")).Select(l =>newLibraryDto{...})
is translated to SQL as expected (WHERE EXISTS . . .). However, when the projection comes first,
dbContext.Set<Library>().Select(l =>newLibraryDto{...}).Where(l =>l.Books.Any(b =>b.Title=="The Cat in the Hat"))
cannot be translated. When .ToList() is not present in the .Select(...), a query like
can be translated, but this is evaluated client-side when .Any() contains a comparison on the collection items or when ToList() is used for the collection navigation property.
Not sure if this is a duplicate issue or a limitation of projection when dealing with correlated subqueries.
Further technical details
EF Core version: 2.1.1
Database Provider: Sqlite in the test project, but affects other providers as well.
Operating system: Windows 10
IDE: Visual Studio 2017 15.7.5, .NET Core SDK 2.1.302.
Projecting into a type that is neither an anonymous type nor a mapped type inside the query does trigger client evaluation. This is a known limitation tracked at #8107.
If you still think that ToList() being present makes a difference, could you please provide some examples? Otherwise I would like to treat this issue as a duplicate of #8107.
// passes[Fact]publicvoidWhereAny_AfterProjectionWithoutToList_WithoutComparison(){vardbSet=this.DbContext.Set<Library>();varquery=this.ProjectWithoutToList(dbSet).Where(l =>l.Books.Any());query.ToList();}// fails:// Error generated for warning 'Microsoft.EntityFrameworkCore.Query.QueryClientEvaluationWarning:// The LINQ expression 'Any()' could not be translated and will be evaluated locally.'.[Fact]publicvoidWhereAny_AfterProjection_WithoutComparison(){vardbSet=this.DbContext.Set<Library>();varquery=this.Project(dbSet).Where(l =>l.Books.Any());query.ToList();}
However I believe .ToList() may be necessary here to avoid N+1 queries by telling EF it can buffer the collection navigation result? Interestingly this test case, which differs from the passing one by using the .Any overload that accepts a predicate, also fails, and the clause it dumps is interesting:
// Error generated for warning 'Microsoft.EntityFrameworkCore.Query.QueryClientEvaluationWarning:// The LINQ expression // 'where {from BookDto b in {// from Book b in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[Issues.OpsAfterProjection.Book]) // where ?= (Property([l], "Id") == Property([b], "LibraryId")) =? // select new BookDto() {Id = [b].Id, Title = [b].Title}} // where ([b].Title == "The Cat in the Hat")// select [b] => Any()// }'// could not be translated and will be evaluated locally.'.[Fact]publicvoidWhereAny_AfterProjectionWithoutToList(){vardbSet=this.DbContext.Set<Library>();varquery=this.ProjectWithoutToList(dbSet).Where(l =>l.Books.Any(b =>b.Title=="The Cat in the Hat"));query.ToList();}
For this predicate, unlike .Any(), projecting with ToList() is also evaluated client-side for basically exactly the same reason (differs from the last one only by a .ToList() after select new BookDto . . .
Error generated for warning 'Microsoft.EntityFrameworkCore.Query.QueryClientEvaluationWarning:
The LINQ expression
'where {from BookDto b in {
from Book b in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[Issues.OpsAfterProjection.Book])
where ?= (Property([l], "Id") == Property([b], "LibraryId")) =?
select new BookDto() {Id = [b].Id, Title = [b].Title}}.ToList()
where ([b].Title == "The Cat in the Hat")
select [b] => Any()
}'
could not be translated and will be evaluated locally.'.
I'm interested to experiment some to see if using anonymous types can provide a workaround for me. In reality I'm using AutoMapper's Queryable extensions to build the projection expression (which includes .ToList()). For the most part .Where, .OrderBy etc seem to work fine in this situation when they reference properties on one-to-one navigation properties, but this is the typical situation where I want to reference a collection navigation.
Activity
divega commentedon Jul 22, 2018
@csboling thanks for raising this.
Projecting into a type that is neither an anonymous type nor a mapped type inside the query does trigger client evaluation. This is a known limitation tracked at #8107.
If you still think that ToList() being present makes a difference, could you please provide some examples? Otherwise I would like to treat this issue as a duplicate of #8107.
csboling commentedon Jul 22, 2018
In the reproduction project there are the following test cases:
where the projection methods being used are:
and the relevant view model is:
However I believe
.ToList()may be necessary here to avoid N+1 queries by telling EF it can buffer the collection navigation result? Interestingly this test case, which differs from the passing one by using the.Anyoverload that accepts a predicate, also fails, and the clause it dumps is interesting:For this predicate, unlike
.Any(), projecting withToList()is also evaluated client-side for basically exactly the same reason (differs from the last one only by a.ToList()afterselect new BookDto . . .I'm interested to experiment some to see if using anonymous types can provide a workaround for me. In reality I'm using AutoMapper's Queryable extensions to build the projection expression (which includes
.ToList()). For the most part.Where,.OrderByetc seem to work fine in this situation when they reference properties on one-to-one navigation properties, but this is the typical situation where I want to reference a collection navigation.csboling commentedon Jul 28, 2018
I've added a few more test cases which show the same behaviors for anonymous types.
ajcvickers commentedon Jul 30, 2018
Triage: we are going to reconsider these behaviors for 3.0, so added a link to #12795 to track with other 3.0 query-related issues.
smitpatel commentedon Sep 1, 2018
Duplicate of #8107