我们使用 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
.Where(c => __selectionCriteria_0
.Any(crit => crit.Id == sc.Id && crit.StatusCode == sc.StatusCode))' 无法翻译。
要么以可翻译的形式重写查询,要么显式切换到客户端评估
插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用。
看https://go.microsoft.com/fwlink/?linkid=2101038 https://go.microsoft.com/fwlink/?linkid=2101038了解更多信息。'
问题似乎在于 .Any 子句中包含 2 个属性。 sql 中存在的 where 通常可以毫无问题地完成此操作。 EFCore 似乎觉得这很困难。
有谁知道如何解决这个问题?
刚刚发现这个;https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/writing-changes#linq-queries-are-no-longer-evaluated-on-the-客户 https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#linq-queries-are-no-longer-evaluated-on-the-client
长话短说;客户端评估在 EFCore 3.1 中不再起作用,这意味着这种类型的查询(将客户端列表与服务器端列表进行比较)不起作用。您需要将其带给客户。我的同事刚才向我指出,我并不欣赏该错误消息的全部潜力:)。
将我的查询更改如下(不是最佳的,但还没有其他解决方案):
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)