0

在下面的代码中,detail是 的列表{ User, Amount, <other fields> },如

var detail = new List<TuitionDetailItem>();

我想要的perMember是一个列表,{ User, Total }其中浮动是Amount每个的总和User

var perMember = detail
    .Where(x => x.ItemType == TuitionDetailItemType.Details)
    .GroupBy(x => new { x.User, x.Amount })
    .Select(x => new { x.Key.User, Total = x.Sum(y => y.Amount) })
    .ToList();

我实际上得到的只是一个detail没有分组或求和的副本。

感谢帮助!!埃里克

4

1 回答 1

0
var perMember = detail
    .Where(x => x.ItemType == TuitionDetailItemType.Details)
    .GroupBy(x => x.User)
    .Select(x => new { x.Key, Total = x.Sum(y => y.Amount) })
    .ToList();

或者更好(我认为它更清洁)

var perMember = from d in detail
    where d.ItemType == TuitionDetailItemType.Details
    group d by d.User into g
    select new { User = g.Key, Total = g.Sum(d => d.Amount)};
于 2012-07-17T00:50:09.050 回答