私は現在、次の作業コードを持っています。しかし、それは痛いほど遅いです。私は、リストとしてではなくクエリとして保有物を渡すことが、物事を単純化し、速めるかもしれないことを知っていますが、いろいろな理由によりこれは不可能です。LINQ to Objects、List内のアイテムを更新する方法は?
新しいリストを作成せずに、保有物のリストを更新するにはどうすればよいですか?コードを改善するための他の提案?個人的に私はでリストを更新しようとしないう
var targetRates = amdw.FXRates
.Where(rate => rate.BaseCurrency == toCurrency)
.ToList();
var query = from holding in holdings
join fxRateHolding in amdw.FXRates
on new { holding.Currency, holding.Date } equals
new { Currency = fxRateHolding.BaseCurrency,
Date = fxRateHolding.ValueDate }
join fxRateToCur in targetRates
on holding.Date equals fxRateToCur.ValueDate
select new { holding, fxRateHolding, fxRateToCur };
を:
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();
}
一般的には、オブジェクトを編集するためにLINQを使用することはありません。 –