In the c#
code snipped below, instead of creating a new tradeContributions
each time, I need to add to this IEnumerable
collection.
I thought I would be able to execute tradeContributions.add()
but the add()
method is not available.
public static IEnumerable<TradeContribution> GetTradeContributions(uint portfolioId, List<uint> portfolioIdList, IEnumerable<DateTime> nodeDateList, int? whatIfBatchNumber = null)
{
// THIS IS THE ORIGINAL CODE
IEnumerable<TradeContribution> tradeContributions = new List<TradeContribution> { };
tradeContributions = (from tc in xml.XPathSelectElement("body/portfolios/portfolio/contributions").Elements("tradeContribution")
select new TradeContribution
{
SysId = tc.Attribute("sysId") == null ? 0 : int.Parse(tc.Attribute("sysId").Value),
TradeContextId = tc.Attribute("contextId") == null ? 0 : int.Parse(tc.Attribute("contextId").Value),
TradeId = tc.Attribute("tradeId") == null ? "" : tc.Attribute("tradeId").Value,
ProductType = tc.Attribute("desc").Value,
ProductDescription = tc.Attribute("desc").Value, // TODO: In future could lookup the description in a reference data cache
Contribution = decimal.Parse(tc.Attribute("contribution").Value)
})
.OrderByDescending(x => x.Contribution);
// ... code omitted for brevity
// THIS IS MY NEW CODE TO HANDLE THE NEW REQUIREMENTS
foreach (XElement pfElem in xml.XPathSelectElements("body/portfolios/portfolio"))
{
tradeContributions = (from tc in pfElem.XPathSelectElement("contributions").Elements("tradeContribution")
select new TradeContribution
{
SysId = tc.Attribute("sysId") == null ? 0 : int.Parse(tc.Attribute("sysId").Value),
TradeContextId = tc.Attribute("contextId") == null ? 0 : int.Parse(tc.Attribute("contextId").Value),
TradeId = tc.Attribute("tradeId") == null ? "" : tc.Attribute("tradeId").Value,
ProductType = tc.Attribute("desc").Value,
ProductDescription = tc.Attribute("desc").Value,
Contribution = decimal.Parse(tc.Attribute("contribution").Value)
}
);
}
return tradeContributions;
}
}
How can I add each new tradeContribution
to my collection?