首页 > 解决方案 > 需要将最大数量的 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 代码通知中可以这样做吗?

标签: c#linqgroupinglinqpad

解决方案


您可以改为创建一个过滤器,该过滤器基于每个组中 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();

推荐阅读