我有“客户”对象列表,我的目标是总结该客户支付了多少钱。
class Customer
{
public string Name {get; set;}
public int Paid {get; set;}
}
例如,如果列表包含以下内容:
[{"Bob",90},{"Tom",120},{"Bob",120,{"Alex",67},{"Tom",40}}]
那么预期结果应该是
[{"Bob",210},{"Tom",160},{"Alex",67}}]
这是我的清单
List<Customer> customers = ...
如何实现
答案 0 :(得分:1)
List<Customer> customers = ...
var summarized = customers
.GroupBy( c => c.Name )
.Select( grp => new
{
Name = grp.Key,
Total = grp.Sum( c => c.Paid )
} )
.OrderByDescending( s => s.Total )
.ToList();