Fel*_*iol 3 c# entity-framework-core asp.net-core-1.0
private decimal GetBankAccountCashierTotal()
{
var company = _context.Company.FirstOrDefault();
return _context.PersonBankAgencyAccount
.Where(p => p.PersonID.Equals(company.PersonID))
.Where(c => c.BankAgencyAccountBalance
.Any(b => b.Reference <= DateTime.Now))
.Select(x => x.BankAgencyAccountBalance
.Where(d => d.Reference.Date <= DateTime.Now)
.OrderByDescending(d => d.Reference)
.FirstOrDefault()
.CurrentBalance)
.sum();
}
Run Code Online (Sandbox Code Playgroud)
这是我的完整方法,在调用此方法时出现异常
Microsoft.EntityFrameworkCore.dll 中出现“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理
和输出
Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler:Error: 迭代查询结果时数据库中发生异常。System.Data.SqlClient.SqlException:无法对包含聚合或子查询的表达式执行聚合函数。
好消息是问题不在您的(绝对有效的)LINQ 查询中。
坏消息是,目前 (v.1.1.0) EF Core LINQ 查询翻译/处理仍然是一场噩梦。经过大量的反复试验,从 EF Core 基础结构中获取不正确的 SQL(因此出现 SQL 异常)或不同的内部异常,我能够使用单个 SQL 获得所需结果且没有异常的唯一(!)方法如下(必须完全这样写):
return _context.PersonBankAgencyAccount
.Where(p => p.PersonID.Equals(company.PersonID))
.SelectMany(p => _context.BankAgencyAccountBalance
.Where(b => b.AccountId == p.Id && b.Reference.Date <= DateTime.Now)
.OrderByDescending(b => b.Reference)
.Take(1))
.Sum(b => b.CurrentBalance);
Run Code Online (Sandbox Code Playgroud)
当然,由于使用导航属性不起作用,我猜了一些名称,如果需要,您可以将它们替换为您的名称。
| 归档时间: |
|
| 查看次数: |
2355 次 |
| 最近记录: |