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

EF Core 5 Grouping subquery under join throws exception #25461

Closed
vovapabyr opened this issue Aug 9, 2021 · 2 comments
Closed

EF Core 5 Grouping subquery under join throws exception #25461

vovapabyr opened this issue Aug 9, 2021 · 2 comments

Comments

@vovapabyr
Copy link

vovapabyr commented Aug 9, 2021

Hello,

After GroupBy() subquery inside .Join() throws the following exception:

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.

We have a file storage system, represented as a regular tree-like structure. Each file can have any number of tags attached to it.
We should be able to search files based on tags. Users can select a number of tags and as a result, they should get a list of files that at least have one of the tags. Here are the domain entities:

public class Data
{
    public string Id { get; set; }
    public string Name { get; set; }
    ...

    public virtual ICollection<Tag> Tags { get; set; }
    ...
}

public class Tag
{
    public string DataId { get; set; }
    public string Value { get; set; }
  
    public virtual Data Data { get; set; }
}

Where (Id) column is the primary key of the Data table and (DataId, Value) is the composite primary key of the Tags table.
And here is the code with which we try to search data based on tags:

var tags = searchModel.Tags.ToList();
var tagsQuery = _context.DataTags.Where(t => tags.Contains(t.Value)).GroupBy(t => t.DataId).Select(gr => gr.Key);
var dataSearchQuery = _context.Data.Join(tagsQuery, d => d.Id, dataId => dataId, (d, dataId) => d);

As you can see we don't group on all key columns, that's why I believe we get this exception and it relates to the breaking change introduced in ef core 5 link. But, the problem is that grouping is critical over here to avoid duplication of data in the search result (when there is a data which has more than one target tags). Also, we need to do it on the server-side as we have millions of files and the above code is part of the server-side pagination which happens further.
Is there any workaround to this problem? Would really appreciate any help.

Many thanks!

EF Core version: 5.0.2
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1
IDE: Visual Studio 2019 16.10.4

@ajcvickers
Copy link
Member

@maumar to find dupe.

@maumar
Copy link
Contributor

maumar commented Aug 17, 2021

dupe of #22049

@maumar maumar closed this as completed Aug 17, 2021
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants