首页 > 解决方案 > EF Core:无法翻译 LINQ 表达式 - Net Core 3.1

问题描述

我正在尝试执行查询以查找每个站点中与分配用户条件匹配的所有条件并将其删除 - 不匹配的条件除外。

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

但是无法翻译查询,导致以下错误:

.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()

如何构建查询以修复错误?

标签: entity-frameworklinqasp.net-core-3.1

解决方案


我能够使用以下似乎可行的代码来解决此问题:

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

推荐阅读