Entity framework EFCore 3.1-通过任何查询存在;无法翻译查询

Entity framework EFCore 3.1-通过任何查询存在;无法翻译查询,entity-framework,linq,exists,any,ef-core-3.1,Entity Framework,Linq,Exists,Any,Ef Core 3.1,我们使用的是EFCore 3.1,并试图通过跨越2个属性的.Any()使用Exists来构建查询 var selectionCriteria = someHugeList.Select(sh => new { sh.Id, sh.StatusCode }).ToList() var resultsQry = _myContext.SomeClass .Include(sc => sc.DetailRecords)

我们使用的是EFCore 3.1,并试图通过跨越2个属性的.Any()使用Exists来构建查询

var selectionCriteria = someHugeList.Select(sh => new { sh.Id, sh.StatusCode }).ToList()
var resultsQry = _myContext.SomeClass
                           .Include(sc => sc.DetailRecords)
                           .Where(sc => selectionCriteria.Any(crit => crit.Id == sc.Id 
                                                                   && crit.StatusCode == sc.StatusCode));

var results = await resultsQry.ToListAsync()
运行此查询时(即使有少量(5项)选择条件项),它也会提供以下错误消息

System.InvalidOperationException:LINQ表达式“DbSet” .其中(c=>\u选择标准\u 0 .Any(crit=>crit.Id==sc.Id&&crit.StatusCode==sc.StatusCode))无法翻译。 以可以翻译的形式重写查询,或者通过 插入对AsEnumerable()、AsAsAsAsyncEnumerable()、ToList()或ToListSync()的调用。 有关更多信息,请参阅

问题似乎在于.Any子句中包含2个属性。sql中的where通常可以毫无问题地完成此操作。EFCore似乎发现这很困难

有人知道如何解决这个问题吗?

刚刚发现了这个问题

长话短说;客户端评估在EFCore 3.1中不再有效,这意味着这种类型的查询(将客户端列表与服务器端列表进行比较)不起作用。您需要将其提交给客户端。我的同事刚才向我指出,我没有充分理解错误消息的潜力:)

将我的查询更改如下(不是最优的,但还没有其他解决方案):


您能否更改
.AsEnumerable()。其中(sc=>selectionCriteria.Any(crit=>crit.Id==sc.Id&&crit.StatusCode==sc.StatusCode))通过
。其中(sc=>Id.Contains(sc.Id)和&code.Contains(sc.StatusCode))
<代码>id
:标准id列表,
代码
:请列出状态代码并对其进行测试?@MohammedSajid的评论与解决方案3:相同。这可能会导致过采样,但最好遵循链接并进行权衡。
var selectionCriteria = someHugeList.Select(sh => new { sh.Id, sh.StatusCode }).ToList()
var resultsQry = _myContext.SomeClass
                           .Include(sc => sc.DetailRecords)
                           .AsEnumerable() // this is the important part, pulling all the records client side so we can execute the .Any on the client.
                           .Where(sc => selectionCriteria.Any(crit => crit.Id == sc.Id 
                                                                   && crit.StatusCode == sc.StatusCode));

var results = await resultsQry.ToList() // no more async, because clientside