将linq总和查询迁移到.net core 3.1

问题描述 投票:0回答:1

我正在从.net core 2.2迁移到3.0,并且在linq查询时出错

        var accountList = someOtherList.Select(x => x.AccountNo).ToList();
        var collectedAmount = await _dbContext.AllocPayments
                .Where(p => accountList.Contains(p.AccountNo))
                .SumAsync(x => x.Amount);

这给出了错误:

System.InvalidOperationException:LINQ表达式'DbSet哪里(a => __accountList_0。包含(a.AccountNo)).Sum(a => a.Amount)'无法翻译。以可以翻译的形式重写查询,或切换到客户端通过插入对AsEnumerable()的调用来显式评估AsAsyncEnumerable(),ToList()或ToListAsync()。看到https://go.microsoft.com/fwlink/?linkid=2101038了解更多信息。

我阅读了文档,可以通过添加.ToListAsync()使其工作并在client/.net中对其进行评估,但是为什么不能在server/sql server上对该查询进行评估?

c# linq-to-sql asp.net-core-3.0
1个回答
0
投票

这是.Net Core 3.0中LINQ大修的一部分。在这里看看,它说明了您遇到此问题的原因:

Announcing Entity Framework Core 3.0 and Entity Framework 6.3 General Availability

© www.soinside.com 2019 - 2024. All rights reserved.