c# - 需要将最大数量的 GroupBy 条件添加到 Linq
问题描述
我已经能够根据 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 <= 500 的总和可以放在这里”的 linq 代码通知中可以这样做吗?
解决方案
您可以改为创建一个过滤器,该过滤器基于每个组中 TotalPaymentAmount 的总和。例如,
paymentGroups = paymentVouchers
.GroupBy(x => new {
x.PayFromBankAccountId,
x.PaymentType,
})
.Where(x=>x.Sum(z=>z.TotalPaymentAmount) <=500) // Add Filtering 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()
}).ToList();
推荐阅读
- c++ - C++ 候选函数不可行:第一个参数没有从“int”到“int *”的已知转换;用 & 取参数的地址
- api - Flutter - 我如何创建 PUBG Uc 采购应用程序?
- shell - How to reduce run time of shell script?
- php - How to send a message to correct user. Laravel chats function using Pusher.ajax
- c++ - Referencing a template from an inner class
- python - How to add gaussian noise to an image in the frequency domain using numpy?
- c++ - Why isn't my binary search giving me a result for the first element?
- c# - How to secure localdb on a standalone application?
- java - Java NumberFormat instance creation
- javascript - How do you add a const variable into document.cookie