我尝试根据条件过滤列表,然后尝试将该条件合并到一条记录中。 我可以通过这种方法实现这一目标:
Dim PortfolioSAA_Bonds = PortfolioSAA.Where(Function(x) x.Description = "Australian Fixed Interest" OrElse x.Description = "International Fixed Interest").ToList()
Dim tmpSAAPortfolio As New SaaPortfolioReportModel
tmpSAAPortfolio.Description = "Bonds"
tmpSAAPortfolio.RiskProfileAllocation = PortfolioSAA_Bonds.Sum(Function(x) x.RiskProfileAllocation)
tmpSAAPortfolio.SAAVariation = PortfolioSAA_Bonds.Sum(Function(x) x.SAAVariation)
tmpSAAPortfolio.ActualSAA = PortfolioSAA_Bonds.Sum(Function(x) x.ActualSAA)
然后
PortfolioSAACustom = PortfolioSAA.Except(PortfolioSAA_Bonds).ToList()
PortfolioSAACustom.Add(tmpSAAPortfolio)
是否可以使用单个LINQ语句将上述值过滤到具有唯一描述的单个记录中(例如我使用' Bonds')。
我尝试使用以下内容,但它没有工作:
Dim ttt = From j In PortfolioSAA
Where j.Description = "Australian Fixed Interest" OrElse j.Description = "International Fixed Interest"
Group By x = New With {Key .Description = "Bonds", Key .RiskProfileAllocation = j.RiskProfileAllocation, Key .SAAVariation = j.SAAVariation, Key .ActualSAA = j.ActualSAA} Into g = Group
Select New SaaPortfolioReportModel With {
.Description = "Bonds",
.RiskProfileAllocation = g.Sum(Function(r) r.RiskProfileAllocation),
.SAAVariation = g.Sum(Function(r) r.SAAVariation),
.ActualSAA = g.Sum(Function(r) r.SAAVariation)
}