如何根据唯一id组合和汇总来自两个列表的结果

本文关键字:两个 结果 列表 唯一 何根 id 组合 | 更新日期: 2023-09-27 18:02:45

我有一个方法,检索两个列表。我正在显示基于并集的列表。

但是我想根据唯一的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;
}

谁能帮我写一个循环语句

如何根据唯一id组合和汇总来自两个列表的结果

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

Aggregate是一个折叠模拟,它迭代你的值并将它们应用于累加器。在本例中,我们提供了一个新字典作为累加器。匿名函数检查字典中是否有你的键;如果有,则将预算值添加到当前值,如果没有,则将键与初始预算值一起添加。它将返回一个Dictionary,其中键是您唯一的FC_Id,其值是这些预算的总和。