【发布时间】:2010-08-19 14:36:02
【问题描述】:
我很难弄清楚如何对 DataTable 执行 LINQ 查询并返回一整行,同时对总和进行 WHERE 子句测试。
我的代码:
transactionsToRemove.AddRange(paymentTransactionResults
.AsEnumerable()
.Where(...)
.GroupBy(r => (decimal)r["AccountNumber"]));
每个 AccountNumber 有多个交易,我需要将它们相加并确定它们是否小于用户输入的金额(出于我的目的,它称为 balanceGreaterThan)。我找不到任何人做过这种事情的例子。
提前致谢,所以。
编辑:抱歉——我需要求和的那一列叫做“余额”
编辑 2:最终代码
transactionsToRemove.AddRange(paymentTransactionResults
.AsEnumerable()
.GroupBy(r => r.Field<string>("AccountNumber"))
.Where(g => g.Sum(r => r.Field<decimal>("Balance")) < balanceGreaterThan)
.SelectMany(g => g));
我不得不更改 GroupBy 以使用 r.Field 而不是 r["AccountNumber"]
【问题讨论】: