I have the following
List<Stock> _investments= new List<Stock>()
{
new Stock {id=msft, price =12, qty=20}
new Stock {id=msft, price =53, qty=50}
new Stock {id=csco, price =222, qty=20}
new Stock {id=fb, price 40, qty=100}
}
Given the symbol, say msft, I want to get the total investment and price. In the above case it will be
id=msft, qty=70, price = (12*20 + 53 *50)/70 = 41.29
How to do it in LINQ? I can get 12 * 20 + 53 * 50 this way:
var c = _investments.Where(i => i.ID == investmentName.ToUpper()).Sum(k => k.InitialPrice * k.Qty);
but how to use qty total effectively
source
share