EF Core: The LINQ expression could not be translated - Net Core 3.1

John Bowyer

I am trying to implement a query to find all Conditions in each Site that match the assigning user's conditions and delete them - excluding the ones that don't match.

var savedPartnerConditions = eipDbContext.EIP_User_Partner_Condition.Where(savedCondition => savedCondition.EIP_User_Partner_Id == savedPartner.EIP_User_Partner_Id && existingUser.CurrentUserConditions.Any(condition => condition.Code == savedCondition.Code));

eipDbContext.EIP_User_Partner_Condition.RemoveRange(savedPartnerConditions);

But the query cannot be translated resulting in the following error:

.Any(condition => condition.Code == e.Code))' 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 either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync()

How can I contruct the query to fix the error?

John Bowyer

I was able to fix this with the following code that seems to work:

var selectionResultSet = eipDbContext2.EIP_User_Partner_Condition.Where(savedCondition => savedCondition.EIP_User_Partner_Id == savedPartner.EIP_User_Partner_Id).ToList();

var savedPartnerConditions = selectionResultSet
.AsEnumerable()
.Where (savedCondition => (existingUser.CurrentUserConditions.Any(condition => condition.Code == savedCondition.Code)));

eipDbContext3.EIP_User_Partner_Condition.RemoveRange(savedPartnerConditions);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

The LINQ expression could not be translated - EF Core

EF Core Linq expression could not be translated

EF Core 3 Linq could not be translated

LINQ Expression could not be translated (Deep Query in EF Core)

The LINQ expression 'DbSet<Entity>.Where(p => p.IsValid)' could not be translated in EF Core 3

Linq expression could not be translated Error Entity Framework Core 3

EF Core 3.1.x : The LINQ expression could not be translated. Either rewrite the query in a form that can be translated

The LINQ expression could not be translated - Entity Framework Core

Ef core 3 casting could not be translated

Migrating from EF Core 2 to EF Core 3 ... could not be translated

InvalidOperationException: The LINQ expression could not be translated, even though I used ToListAsync() - EF Core 3.1

EF core query could not be translated

EF Core CreateDateTime could not be translated

ASP.NET Core Web API controller; the LINQ expression could not be translated

The Query (LINQ) expression could not be Translated Entity Framework Core

EF Linq GroupBy expression could not be translated

EF Core could not be translated and will be evaluated locally

Expressions with LinqKit could not be translated - EF Core

EF Core 5 - LINQ cannot be translated

The LINQ expression could not be translated

LINQ expression could not be translated

EntityFramework Core LINQ expression unable to be translated to SQL

Expression could not be translated error when using a local list to with Any clause in EF Core

EF Core 3.0 Linq decimal compare (Greater or Less than) not working - could not be translated

EF Core "Group By could not be translated and will be evaluated locally."

EF Core ValueObject comparison could not be translated and will be evaluated locally

EF Core 3.x - simple LINQ with Include can not be translated, client evaluation

The LINQ expression could not be translated and will be evaluated

InvalidOperationException: The LINQ expression could not be translated