I currently have the following working code. It is painfully slow however. I know that passing the holdings as a query rather than as a list might simplify and speed up things, but for various reasons this is not possible.
How can I update the list of holdings in place, without creating a new list? Other suggestions to improve the code?
internal List<DailyHoldingItem> TransformHoldingItemsToCurrency(List<DailyHoldingItem> holdings,string toCurrency)
{
//TODO - how to do this calculation in place, without creating a new list?
var query = (from holding in holdings
from fxRateHolding in amdw.FXRates
from fxRateToCur in amdw.FXRates
where
fxRateHolding.BaseCurrency == holding.Currency &&
fxRateToCur.BaseCurrency == toCurrency &&
fxRateHolding.ValueDate == holding.Date &&
fxRateToCur.ValueDate == holding.Date
select new { holding, fxRateHolding, fxRateToCur });
return query.Select(dhi =>
{
decimal factor = dhi.fxRateToCur.Value / dhi.fxRateHolding.Value;
dhi.holding.MarketValue *= factor;
dhi.holding.Fee *= factor;
dhi.holding.Remuneration *= factor;
return dhi.holding;
}).ToList();
}
Well for one thing you can speed things up using joins, and only evaluating the valid “target” currencies once:
Personally I wouldn’t try to update the list in place and I wouldn’t mutate the existing holdings (as you currently do in your
Selectcall). Mutating existing values tends to make your code harder to reason about – which is why LINQ is designed to be used in a more functional way.