我正在使用此linq查询来获取双打列表。该列表看起来正确,但现在我需要在列表中总计双打。
var total = from sauId in schools
join item in entities.AmountTable
on sauId equals item.sauId
select item.amount;
答案 0 :(得分:3)
对结果使用Sum()
:
var total = (from sauId in schools
join item in entities.AmountTable
on sauId equals item.sauId
select item.amount).Sum();
如果item.amount
是double
,则total
也是double
。
答案 1 :(得分:0)
对于任何寻求流利语法的人,我相信这将是这样做的方式:
var total = schools
.Join(
entities.AmountTable,
s => s.sauId,
at => at.sauId,
(s, at) => at.amount)
.Sum();
答案 2 :(得分:0)
尝试按sauId
:
var qry = from item in entities.AmountTable
group item by item.sauId into grp
select new {
sauId = grp.Key,
amuont = grp.Sum(c=>c.amount)
};