gpt4 book ai didi

entity-framework - EFCore 3.1 - 通过 Any 存在查询;查询无法翻译

转载 作者:行者123 更新时间:2023-12-04 13:38:53 24 4
gpt4 key购买 nike

我们正在使用 EFCore 3.1 并尝试使用 Exists 通过跨越 2 个属性的 .Any() 构建查询。

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 expression 'DbSet .Where(c => __selectionCriteria_0 .Any(crit => crit.Id == sc.Id && crit.StatusCode == sc.StatusCode))' 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(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'



似乎问题在于 .Any 子句中包含 2 个属性。 sql 中存在的 where 通常可以毫无问题地执行此操作。 EFCore 似乎觉得这很难。

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

最佳答案

刚发现这个; https://docs.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

关于entity-framework - EFCore 3.1 - 通过 Any 存在查询;查询无法翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60092959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com