【发布时间】:2020-12-04 23:18:34
【问题描述】:
我已经能够获得基于 2 列的分组,但是当金额不大于指定的金额时,我需要按第三个条件分组
在这里我建立了测试数据
Linqpad 中的结果如下所示:
Group PayFromBankAccountId PaymentType TotalAmounts TotalCount
---------------------------------------------------------------
3 ABC 5 600 3
3 ABC 6 600 3
2 DEF 5 300 2
2 DEF 6 300 2
问题是我不希望它有任何总金额超过 500 的组
类
public class PaymentGroups
{
public Int32 Group {get; set;}
public string PayFromBankAccountId { get; set; }
public int PaymentType { get; set; }
public decimal TotalAmounts { get; set; }
public int TotalCount { get; set; }
}
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, TotalPaymentAmount = 100},
new PaymentVouchers { PayFromBankAccountId = "ABC", PaymentType = 5, TotalPaymentAmount = 200},
new PaymentVouchers { PayFromBankAccountId = "ABC", PaymentType = 5, TotalPaymentAmount = 300},
new PaymentVouchers { PayFromBankAccountId = "ABC", PaymentType = 6, TotalPaymentAmount = 100},
new PaymentVouchers { PayFromBankAccountId = "ABC", PaymentType = 6, TotalPaymentAmount = 200},
new PaymentVouchers { PayFromBankAccountId = "ABC", PaymentType = 6, TotalPaymentAmount = 300},
new PaymentVouchers { PayFromBankAccountId = "DEF", PaymentType = 5, TotalPaymentAmount = 100},
new PaymentVouchers { PayFromBankAccountId = "DEF", PaymentType = 5, TotalPaymentAmount = 200},
new PaymentVouchers { PayFromBankAccountId = "DEF", PaymentType = 6, TotalPaymentAmount = 100},
new PaymentVouchers { PayFromBankAccountId = "DEF", PaymentType = 6, TotalPaymentAmount = 200},
};
var paymentGroups = new List<PaymentGroups>();
paymentGroups = paymentVouchers
.GroupBy(x => new {
x.PayFromBankAccountId,
x.PaymentType,
// Sum of TotalPaymentAmount <= 500 Can this go here
})
.Select(x => new PaymentGroups
{
PayFromBankAccountId = x.Key.PayFromBankAccountId,
PaymentType = x.Key.PaymentType,
TotalAmounts = x.Sum(z => z.TotalPaymentAmount),
TotalCount = x.Count(),
Group = x.Count() // unsure ??
}).ToList();
paymentGroups.Dump("final");
所以我想要拥有最大计数为 500 的新组,所以也许最终表中的 Group 列需要知道组整数值 - 也许这还需要一些其他 ID?
我想做什么?按金额总和的附加列分组,以便它们不能超过 500,因此在我放置“// TotalPaymentAmount
【问题讨论】:
-
您要求“另一个 GroupBy”,但看起来您真正想要的只是过滤您已有的组?
.Where(e => e.TotalAmounts < 500)(在你的决赛之前.ToList()) -
您能否将您给出的特定输入的理想输出作为示例包含在内?