'EF Core 6 - when using Contains in the where clause causes an error "could not be translated"
I am migrating from EF Core 2.1 to EF Core 6.
I have the following code:
var currentCars = _context.Cars
.Include(x => x.Model)
.Where(x => currentModels.Contains(x.ModelId))
.ToList();
Cars have a ModelId(long) that relates to a Model type.
CurrentModels is a List<long>, being the current model type Ids we are interested in.
The LINQ expression 'EnumerableQuery { 1 }
.Contains(NavigationTreeExpression
Value: EntityReference: AssetSP | IncludePaths:
Root
-> Model
Expression: a.ModelId)' 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.
I can't work out how to do the query on the database. I do not want to bring all the Cars back to the server to filter there.
In SQL it would be something like.
Select *
From Cars
Where ModelId in (-- ids here --)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
