C# 属性列表。需要根据 2 个条件对它们进行分组

C# List of properties. Need to group them based on 2 conditions

我有一个数据库数据列表,其中有 2 个属性对于分组很重要。我删除了不重要的属性并创建了一个需要对其进行排序的数据列表。

不确定是否应该使用带有 linq 语句的算法的某些方法,然后最好如何存储结果?

public class PaymentVouchers
{
    public string PayFromBankAccountId {get; set; }
    public int PaymentType {get; set;}
    public decimal TotalPaymentAmount {get; set;}
}

数据:

var paymentVouchers = new List<PaymentVouchers>()
{
    new PaymentVouchers { PayFromBankAccountId = "ABC", PaymentType = 5},
    new PaymentVouchers { PayFromBankAccountId = "ABC", PaymentType = 5},
    new PaymentVouchers { PayFromBankAccountId = "ABC", PaymentType = 5},
    new PaymentVouchers { PayFromBankAccountId = "ABC", PaymentType = 6},
    new PaymentVouchers { PayFromBankAccountId = "ABC", PaymentType = 6},
    new PaymentVouchers { PayFromBankAccountId = "ABC", PaymentType = 6},
    new PaymentVouchers { PayFromBankAccountId = "DEF", PaymentType = 5},
    new PaymentVouchers { PayFromBankAccountId = "DEF", PaymentType = 5},
    new PaymentVouchers { PayFromBankAccountId = "DEF", PaymentType = 6},
    new PaymentVouchers { PayFromBankAccountId = "DEF", PaymentType = 6},
}

因此根据数据,我需要将 ABC 和 5 组合在一起,因为其中有 3 个,然后将 ABC 和 6 组合在一起,偶然也有 3 个。然后在数据中看到另外 2 个组 DEF 和 5 - 2 个和 DEF 和 6,其中有 2 个。

因此,如果我执行了一个 foreach 循环,一个 linq 语句 - 我如何防止它们因将它们分组到哪种类型的集合而变得混乱?元组的哈希集?一些带有新 属性 的新列表,它是一个标识符 "key" 。性能理想

想法?

添加:更新:

我无法将列表中的数据放入新列表

var paymentGroups = new List<PaymentGroups>();



paymentGroups = paymentVouchers.GroupBy(x => new { x.PayFromBankAccountId, x.PaymentType })
.Select(x => new
{
    x.Key.PayFromBankAccountId,
    x.Key.PaymentType,
    //x.Sum(PaymentType)
    Sum = x.Sum(TotalPaymentAmount),
    paymentGroups.TotalCount = x.Key.Count 
});


paymentGroups.Dump();

}

public class 付款组 {

public string PayFromBankAccountId { get; set; }
public int PaymentType { get; set; }
public decimal TotalAmounts { get; set; }
public int TotalCount {get; set; }

}

您可以像这样在分组依据上使用 2 个属性:

paymentVouchers.GroupBy(x => new { x.PayFromBankAccountId, x.PaymentType }).Select(x => new {
  x.Key.PayFromBankAccountId,
  x.Key.PaymentType,
  x.Sum(/*Something*/)
  // REST
})

编辑:

paymentGroups = paymentVouchers.GroupBy(x => new { x.PayFromBankAccountId, x.PaymentType })
.Select(x => new 
{
    x.Key.PayFromBankAccountId,
    x.Key.PaymentType,
    TotalAmounts = x.Sum(TotalPaymentAmount),
    TotalCount = x.Count() 
});

这将 return 一个由 2 个键及其值和每组的 TotalPaymentAmount 总和以及每组计数分组的列表。

如果您想输入您定义的 class 那么:

paymentGroups = paymentVouchers.GroupBy(x => new { x.PayFromBankAccountId, x.PaymentType })
.Select(x => new PaymentGroups
{
    PayFromBankAccountId = x.Key.PayFromBankAccountId,
    PaymentType = x.Key.PaymentType,
    TotalAmounts = x.Sum(TotalPaymentAmount),
    TotalCount = x.Count() 
});

使用ValueTuple进行分组。很难与其性能特征竞争。请注意,LINQ 对性能而言并不理想。

List<(string Group, int Total, int Amount)> data = paymentVouchers
    .GroupBy(v => (v.PayFromBankAccountId, v.PaymentType))
    .Select(g => (g.Key, Vouchers: g.ToList()))
    .Select(t => (Group: $"{t.Key.PayFromBankAccountId}|{t.Key.PaymentType}", Total: t.Vouchers.Count, Amount: t.Vouchers.Sum(x => x.PaymentType)))
    .ToList();