在下面的代码中,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
的副本,没有分组或总结。
感谢您的帮助!! 埃里克
答案 0 :(得分: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)};