c# - 在 C# 中使用 linq 合并列表

标签 c# linq

public class APIBillingHistory
{        
    public List<APIBillingHistoryDetails> BillingHistoryDetails;
}

public class APIBillingHistoryDetails
{   
    public List<APIBillingHistoryPaymentType> PaymentType;
    public string BillId;
}

public class APIBillingHistoryPaymentType
{
    public string Description;
    public Decimal Principal;
}

我有一类嵌套列表对象。我想将相应的 PaymentList 集合合并到其父列表 APIBillingHistoryDe​​tails

例如:

APIBillingHistory
-----BillingHistoryDetails
        Bill ID : 123
        ----PaymentType
                Description : "A"
                Principal : 100
        ----PaymentType
                Description : "B"
                Principal : 200
-----BillingHistoryDetails
        Bill ID : 123
        ----PaymentType
                Description : "A"
                Principal : 150
        ----PaymentType
                Description : "B"
                Principal : 300

假设我在上面指定了样本日期。我希望得到以下格式的结果。在这里,我通过添加 Principal 属性来合并 PaymentList 每个 描述 值(如果它们具有相同的帐单 ID

)

输出应如下所示:

APIBillingHistory
-----BillingHistoryDetails
        Bill ID : 123
        ----PaymentType
                Description : "A"
                Principal : 250
        ----PaymentType
                Description : "B"
                Principal : 500

最佳答案

这将给出所需的输出

var merged = new APIBillingHistory {
    BillingHistoryDetails = history
        .BillingHistoryDetails
        .GroupBy(detail => detail.BillId) //Group same bill Ids
        .Select(detailGroup => new APIBillingHistoryDetails {
            BillId = detailGroup.Key,
            PaymentType = detailGroup.SelectMany(p => p.PaymentType) //Get all payments
                .GroupBy(pymtType => pymtType.Description) //and group them by description
                .Select(pymtTypeGroup => new APIBillingHistoryPaymentType { //construct payment type
                    Description = pymtTypeGroup.Key,
                    Principal = pymtTypeGroup.Sum(t => t.Principal) // summing all grouped principals
                }).ToList()
        }).ToList()
};

给定

var history = new APIBillingHistory {
    BillingHistoryDetails = new List<APIBillingHistoryDetails> {
         new APIBillingHistoryDetails {
              BillId = "123",
              PaymentType = new List<APIBillingHistoryPaymentType>{
                  new APIBillingHistoryPaymentType {
                     Description = "A",
                     Principal = 100
                  },
                  new APIBillingHistoryPaymentType {
                     Description = "B",
                     Principal = 200
                  }
              }
         },
         new APIBillingHistoryDetails {
              BillId = "123",
              PaymentType=new List<APIBillingHistoryPaymentType>{
                  new APIBillingHistoryPaymentType {
                     Description = "A",
                     Principal = 150
                  },
                  new APIBillingHistoryPaymentType {
                     Description = "B",
                     Principal = 300
                  }
              }
         }
     }
};

关于c# - 在 C# 中使用 linq 合并列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44664195/

相关文章:

c# - 分层数据的高级LINQ分组和投影查询(EF 4.0 + LINQ + ASP.NET MVC + HighCharts)

c# - 使用 C# 去除扫描图像中的黑色区域

c# - 如何从另一个事件/类中调用 Invalidate 而不是整个面板

c# - ASP.NET MVC : how to make all unspecified URLs direct to one controller

c# - Linq to Objects 按任意数量的参数排序

c# - Linq to Objects 通用复制方法

c# - Linq SqlMethods 不喜欢

.net - 引用匿名类型属性

Linq to NHibernate 和 Dynamic LINQ - 查询缓存不工作

c# - SqlDataAdapter 填充错误