As a follow-up to this question of mine I have got this SQL:
SELECT Documents.*
FROM Documents
WHERE Documents.ID IN
(
SELECT Keywords.DocumentID
FROM Keywords
WHERE
Keywords.Keyword = 'KeywordA' OR
Keywords.Keyword = 'KeywordB'
GROUP BY Keywords.DocumentID
HAVING COUNT(Keywords.Keyword) = 2
)
I did use Linqer to convert the query to C# to use with Entity Framework Core 5:
from Document in db.Document
where
(from Keyword in db.Keyword
where
Keyword.Value == "KeywordA" ||
Keyword.Value == "KeywordB"
group Keyword by new {
Keyword.DocumentId
} into g
where g.Count(p => p.Value != null) == 2
select new {
g.Key.DocumentId
}).Contains(new { DocumentId = Document.DocumentId })
select Document
This compiles successfully, but upon running the query, I get an error:
The LINQ expression '
<see below>' 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
The formatted LINQ expression from the above error message reads:
DbSet<Keyword>()
.Where(k => k.Value == "KeywordA" || k.Value == "KeywordB")
.GroupBy(
keySelector: k => new { DocumentId = k.DocumentId },
elementSelector: k => k)
.Where(e => e
.Count(p => p.Value != null) == 2)
.Select(e => new { DocumentId = e.Key.DocumentId })
.Any(p => p == new { DocumentId = EntityShaperExpression:
EntityType: Document
ValueBufferExpression:
ProjectionBindingExpression: EmptyProjectionMember
IsNullable: False
.DocumentId })
I really do not understand what's wrong here. I could only imagine that Linqer is too old to generate valid C# code for use in EF Core 5.
My question
Could someone give me a hint on what I'm doing wrong here and how to resolve the issue? (I.e. how to rewrite the C# query)
select new {g.Key.DocumentId}).Contains(...)you tryselect 1).Any()