question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to translate collection subquery in projection

See original GitHub issue

I have come across an issue where I’m unable to construct a suitably performant query with a group by clause. I have seen a simliar issue with the same error message. However, all models have a key. I can rewrite the query used to use Contains instead of a join, but the query produced is 100x slower (30 seconds instead of 0.3 seconds) so I’m not sure where to go from here. I have attached a minimal example efcore-sample.zip

The error message is as follows:

Unable to translate collection subquery in projection since the parent query doesn’t project key columns of all of it’s tables which are required to generate results on client side. This can happen when trying to correlate on keyless entity or when using ‘Distinct’ or ‘GroupBy’ operations without projecting all of the key columns.

Unfortunately in this small example I can’t easily generate enough data to show the performance issue in the second query (Manually rewriting the query using IN (Select .....) as opposed to WHERE EXISTS(....HAVING) is also orders of magnitude quicker but I was unable to generate a query which used the IN operator) but in the real dataset the “books” table has 7475 rows and the “bookauthors” table has 31557 rows. ThenInclude isn’t used so Authors isn’t actually relevant

Include provider and version information

EF Core version: 5.08 Database provider: Npgsql.EntityFrameworkCore.PostgreSQL Target framework: .NET 5.0 Operating system: Windows 10 IDE: Visual Studio Code 1.58.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
smitpatelcommented, Sep 10, 2021

@roji - This uses preview6 packages with npgsql provider. Can you try the sample app on latest release?

0reactions
maumarcommented, Sep 14, 2021

TODO: add regression test

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to translate a collection subquery in a projection... ...
Hello team I have this piece of code that is working fine on EF Core 5, and previously on v3: This is a...
Read more >
Getting error 'Unable to translate a collection subquery in ...
but when EF Core wants to create a query for SQL, it throws this exception: Unable to translate a collection subquery in a...
Read more >
EF6 include collection subquery of images is failing me
System.InvalidOperationException: 'Unable to translate a collection subquery in a projection since either parent or the subquery doesn't project ...
Read more >
In this article
Unable to translate collection subquery in projection since the parent query doesn't project key columns of all of it's tables which are required...
Read more >
Linq query system.invalidoperationexception
Unable to translate collection subquery in projection since the parent query doesn't project key columns of all of it's tables which are ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found