2017-06-20 103 views
2
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在c中使用linq合併列表#

例如:

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每個Description值,如果他們有同一法案編號

輸出應該是這樣的:

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

我相信你自己嘗試過。你能指出你卡在哪裏嗎? –

回答

3

這將給期望輸出

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 
        } 
       } 
     } 
    } 
}; 
2

我知道它的醜陋,但至少它的工作原理:)

List<APIBillingHistoryDetails> newList = (from item in BillingHistoryDetails.GroupBy(t => t.BillId) 
    let paymentType = item 
     .SelectMany(t => t.PaymentType) 
     .GroupBy(t => t.Description) 
     .Select(t => new APIBillingHistoryPaymentType 
     { 
      Description = t.Key.Description, 
      Principal = t.Sum(s => s.Principal) 
     }) 
     .ToList() 
    select new APIBillingHistoryDetails 
    { 
     BillId = item.Key, 
     PaymelntType = paymentType 
    } 
).ToList(); 
0

所以,你有一個ApiBillingHistory,它具有零個或多個ApiBillingHistoryDe​​tails,而且每個APIBillingHistoryDe​​tail具有零個或多個APIBillingHistoryPaymentTypes。

而且您想將具有相同ID的所有ApiBillingHistoryDe​​tails組合到一個ApiBillingHistoryDe​​tail中,其中PaymentType是所有具有相同描述的ApiBillingHistoryPaymentTypes的組以及組中元素的所有主體值的總和。

如果一個ApiBillingHistoryDe​​tail包含不在任何其他ApiBillingHistoryDe​​tail中的ID,您沒有提及您想要的內容。如果一個PayMentType描述不在其他paymentTypes中,也不是。

// take all BillingHistoryDetails 
var groups = ApiBillingHistory.BillingHistoryDetails 
    // group them into groups of items with the same Id 
    .GroupBy(
     // Key of the group is this same Id 
     historyDetail => historyDetail.Id, 
     // elements of the group: 
     // group all payment types of the history detail 
     // into subgroups of items with the same Description 
     historyDetail => historyDetail.PaymentTypes.GroupBy( 
       // Key of subgroup: the Description         
       paymentType => payMentType.Description, 
       // elements of the gorup = the Principal 
       // of the paymentTypes with this description 
       paymentType => paymentType.Principal), 

      // now we have sub groups of Principals from all paymentTypes 
      // with a Description equal to the sub group. 
      // convert every sub Group into one PaymentType: 
      .Select(subGroup => new PaymentType() 
      { 
       Description = subGroup.Key, 
       Principal = subGroup.Sum(), 
      })) 

     // so now we have Groups 
     // with a Key historyDetail.Id 
     // and as elements the list of PaymentTypes 
     // where each payMentType contains the Description and the sum of all 
     // payment types with this description 
     // convert to billingHistoryDetails: 
     .Select(mainGroup => new BillingHistoryDetails 
     { 
      // Id is the Key of the group 
      Id = mainGroup.Key, 
      // PaymentType is the list of created PaymentTypes 
      PaymentType = mainGroup.ToList(), 
     });