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

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集合合并到其父列表APIBillingHistoryDetails

例如:

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

假设我有上面指定的采样日期。我希望产生以下格式。在这里,我通过为每个PaymentList值(如果它们具有相同的帐单ID)添加Principal属性来合并Description

输出应如下所示:

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#

以下代码向我警告了IEnumerable可能的多个枚举:public ClassName(IEnumerable<OtherClassName> models) : base(models) { _models.AddRange(models); } 由于“基本”调用,消除此警告的常规解决方案不起作用。我无法转换为列表,因为没有地方可以存储该列表…

字段初始化程序访问“this”:在C#中无效,在Java中有效? - c#

Improve this question 一,简介:这段代码:class C { int i = 5; byte[] s = new byte[i]; } 无法编译,并出现以下错误: 字段初始值设定项不能引用非静态字段,方法或属性“C.i”Resharper说类似的话:无法在静态上下文中访问非静态字段i。这与C# spec says一致-字段初始值设定项无…

每个文件合并后添加换行 - python

我有很多类似以下内容的JSON文件:例如。1.json{"name": "one", "description": "testDescription...", "comment": ""} test.json{"name"…

在C++之后学习C# - c#

随着语言的发展,我一直在学习C和C++。现在,我想学习C#。我知道它们之间有一些巨大的差异-例如删除指针和垃圾回收。但是,我不知道两者之间的许多差异。C++程序员在转移到C#时需要知道的主要区别是什么? (例如,我可以使用什么代替STL,它们之间的语法差异或任何其他可能被认为重要的东西。) 参考方案 C# for C++ Developers是一个不错的起点…

在C#中调用C++库 - c#

Improve this question 我有很多用C++编写的库。我想从C#中调用这些库,但是遇到了很多问题。我想知道是否有一本书或指南可以告诉我如何做。 参考方案 DllImport-http://msdn.microsoft.com/en-us/library/aa984739(VS.71).aspx 包装器类-http://social.msdn.…