【发布时间】:2021-10-06 20:54:50
【问题描述】:
我有如下查询:
var query = from operation in dbContext.Operations
select new OperationsDto
{
Id = o.Id,
ProcessDate = o.ProcessDate,
Amount = o.Credit
//Balance = ...
};
Operation 实体中没有 Balance 属性。我必须计算一下。 余额 => (操作总和在当前操作 ProcessDate 之前发生的操作的金额) + 当前金额)。例如:
Id ProcessDate Amount Balance
1 2021.02.01 +100$ 50 + 100 = +150$ (
2 2021.02.03 -200$ 150$ + (-200) = -50$ (this get sum of amount where record's process date before 2021.02.03)
3 2019.01.01 +50$ 0 + 50$ = 50$ (because this is first operation. there is not record before 2019.01.01)
我想在 EF Core 中使用它。 我知道我可以在检索如下数据后使用 foreach 执行此操作:
var operations = query.ToList();
foreach (var operation in operations)
{
operation.Balance = operations.Where(x => x.ProcessDate < operation.ProcessDate).Sum(o => o.Debit - o.Credit);
}
但我需要在查询中计算
【问题讨论】:
-
您不能在 Linq 中真正做到这一点,而且您的
foreach循环效率极低。你真正想做的是保持“运行总数”,所以我建议你去阅读。您可以使用一些原始 SQL 来执行此操作,或者在按日期对结果进行排序后使用 this 之类的内容。 -
所以您实际上是在谈论与每个操作相关的运行余额。初始余额好像是从第一条记录设置的?
-
@NetMage 是的,先根据processDate记录
标签: c# sql entity-framework linq entity-framework-core