2016-08-17 208 views
0

我有一個方法來檢索兩個列表。 和我顯示基於聯盟的列表。如何結合並總結基於唯一ID的兩個列表的結果

,但我想總結基礎上,唯一的ID是FC_Id

public List<ContractFundingCategory> CalculateContractFundingCategoryAmountbyPurchaseOrder(int poId, string serviceType) 
{ 
    List<ContractFundingCategory> lstContractFundingCategory = new List<ContractFundingCategory>(); 

    var lstContractFundingCategoryEbs = 
     (
      from payment in context.PaymentDetails 
      join reimEbs in context.Reimbursement_EBSUtilization on payment.REU_Id equals reimEbs.Id into ebs_join 
      from reimEbs in ebs_join.DefaultIfEmpty() 
      join purc in context.PurchaseOrders on payment.PO_Id equals purc.Id 
      join serviceDetail in context.fServiceDetails on reimEbs.SD_Id equals serviceDetail.Id 
      join fundingCategory in context.fFundingCategories on serviceDetail.FC_Id equals fundingCategory.Id into fundingCategory_Join 
      from fundingCategory in fundingCategory_Join.DefaultIfEmpty() 
      where payment.PO_Id == poId && (serviceDetail.ServiceType == serviceType) 
      group new { fundingCategory, payment, serviceDetail } by new 
      { 
       fundingCategory.Id, 
       serviceDetail.ServiceType, 
      } into grp 
      select new 
      { 
       FC_Id = grp.Key.Id, 
       serviceType = grp.Key.ServiceType, 
       BudgetAmount = grp.Sum(p => p.payment.PaymentAmount), 
      } 
     ) 
     .AsEnumerable().Select(x => new ContractFundingCategory 
     { 
      FC_Id = x.FC_Id, 
      BudgetAmount = x.BudgetAmount, 
      ServiceType = x.serviceType.ToString() 
     }).ToList(); 


    var lstContractFundingCategoryCds = 
     (
      from payment in context.PaymentDetails 
      join reimCds in context.Reimbursement_CDSUtilization on payment.RCU_Id equals reimCds.Id into cds_join 
      from reimCds in cds_join.DefaultIfEmpty() 
      join cdsutil in context.CDSUtilizations on reimCds.CDSU_Id equals cdsutil.Id 
      join purc in context.PurchaseOrders on payment.PO_Id equals purc.Id 
      join serviceDetail in context.fServiceDetails on cdsutil.ServiceDetail_Id equals serviceDetail.Id 
      join fundingCategory in context.fFundingCategories on serviceDetail.FC_Id equals fundingCategory.Id into fundingCategory_Join 
      from fundingCategory in fundingCategory_Join.DefaultIfEmpty() 
      where payment.PO_Id == poId && (serviceDetail.ServiceType == serviceType) 
      group new { fundingCategory, payment, serviceDetail } by new 
      { 
       fundingCategory.Id, 
       serviceDetail.ServiceType, 
      } into grp 
      select new 
      { 
       FC_Id = grp.Key.Id, 
       serviceType = grp.Key.ServiceType, 
       BudgetAmount = grp.Sum(p => p.payment.PaymentAmount), 
      } 
     ) 
     .AsEnumerable().Select(x => new ContractFundingCategory 
     { 
      FC_Id = x.FC_Id, 
      BudgetAmount = x.BudgetAmount, 
      ServiceType = x.serviceType.ToString() 
     }).ToList(); 

    //lstContractFundingCategory.AddRange(lstContractFundingCategoryEbs); 
    //lstContractFundingCategory.AddRange(lstContractFundingCategoryCds); 

    //List<ContractFundingCategory> a = new List<ContractFundingCategory>(); 

    lstContractFundingCategory = lstContractFundingCategoryEbs 
     .Union(lstContractFundingCategoryCds) 
     .ToList(); 

    return lstContractFundingCategory; 

} 

任何一個可以幫助我走出編寫循環語句

+6

作爲一般性評論:請不要將您的代碼複製並粘貼到SO中。花點時間把它縮小到足夠小,這樣人們不必爲了看看你做了什麼而水平滾動太多。 – phil13131

回答

0
lstContractFundingCategory.Aggregate(new Dictionary<int,int>(), (acc,elem) => { acc.ContainsKey(elem.FC_Id) ? acc[elem.FC_Id] += elem.BudgetAmount : acc.Add(elem.FC_Id, elem.BudgetAmount); return acc; }); 

總金額爲摺疊類似物,其迭代你的值並將它們應用到累加器。在這種情況下,我們提供了一個新的字典作爲累加器。匿名函數檢查字典是否有你的密鑰;如果確實如此,則它將預算值添加到當前值,並且如果它不添加具有初始預算值的密鑰。它將返回一個字典,其中的鍵是唯一的FC_Id,它們的值是這些預算的總和。