我有點問題,因爲我試圖GroupBy使用LINQ,雖然它的工作原理,它只適用於當我消除代碼的一個元素。Linq Groupby的問題沒有總結
nestedGroupedStocks = stkPositions.GroupBy(x => new { x.stockName,
x.stockLongshort,x.stockIsin, x.stockPrice })
.Select(y => new stockPos
{
stockName = y.Key.stockName,
stockLongshort = y.Key.stockLongshort,
stockIsin = y.Key.stockIsin,
stockPrice = y.Key.stockPrice,
stockQuantity = y.Sum(x => x.stockQuantity)
}).ToList();
上面的代碼組我的股票倉位,並在含47項,但它不能做的是和重複的個股有不同批量的單子,結果...
nestedGroupedStocks = stkPositions.GroupBy(x => new { x.stockName,
x.stockIsin, x.stockPrice })
.Select(y => new stockPos
{
stockName = y.Key.stockName,
stockIsin = y.Key.stockIsin,
stockPrice = y.Key.stockPrice,
stockQuantity = y.Sum(x => x.stockQuantity)
}).ToList();
但是,如果我elimanate「x.longshort」然後我得到了想要的結果,34只個股總結出來的,但當時的列表中的所有longshort元素是空...
其駕駛我堅果:-)
發佈你的清單數據 –