John Bowyer
John Bowyer

Reputation: 1493

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

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?

Upvotes: 1

Views: 956

Answers (1)

John Bowyer
John Bowyer

Reputation: 1493

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);

Upvotes: 1

Related Questions