从列表中获取不同的数据并添加值

Get distinct data from list and add vlaues

假设我在 C# 中有 class 调用这样的会计:

 public class Accounting 
{
    public string Customer;
    public string Payment;
}

如果我有会计清单 class

List<Accounting> AccountList = new List<Accounting>();

如果 AccountList 有这样的数据:

Joe : 100
Ali : 200
Machel : 100
Joe : 200
Machel : 500

我只想在列表中获取不同的数据,并像那样为每个客户收集付款

Joe : 300
Ali : 200
Machel : 600

据我所知,您希望通过 Accounting.Customer GroupBy 然后 Sum 所有 Accounting.Payment:

using System.Linq;

...

var result = AccountList
  .GroupBy(item => item.Customer, item => item.Payment)
  .Select(group => new Accounting() {
     Customer = group.Key,
     Payment = group.Sum(item => decimal.Parse(item)).ToString() 
   })
  .ToList();

请注意,string 存钱是一种不好的做法;尝试将 decimal 用于 Payment.