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;
}
任何一個可以幫助我走出編寫循環語句
作爲一般性評論:請不要將您的代碼複製並粘貼到SO中。花點時間把它縮小到足夠小,這樣人們不必爲了看看你做了什麼而水平滾動太多。 – phil13131