【问题标题】:GroupBy condition with max amount needed to be added to Linq需要将最大数量的 GroupBy 条件添加到 Linq
【发布时间】: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 =&gt; e.TotalAmounts &lt; 500)(在你的决赛之前.ToList()
  • 您能否将您给出的特定输入的理想输出作为示例包含在内?

标签: c# linq grouping linqpad


【解决方案1】:

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

【讨论】:

  • 好的,我明白了。如果我仍然想让这些剩余的出现在结果中怎么办 - 但只要它们低于 500 。所以现在这个查询的例子只返回 DEF 的 BankAccounts ,因此有 2 条记录 - 那是因为它们总共有 300 条,但是如果我也想要那里的其他记录,那么 ABC 5 100 ,然后是 ABC 5 200 和 ABC 5 300 - 理想情况下,我想将 100 和 200 组合在一起,但 300 必须单独使用
猜你喜欢
  • 1970-01-01
  • 2012-02-28
  • 1970-01-01
  • 2018-07-16
  • 1970-01-01
  • 1970-01-01
  • 2018-07-17
  • 2011-10-07
  • 1970-01-01
相关资源
最近更新 更多