1
我有一個LINQ查詢一樣跟隨着獲得第一組:LINQ由多個組,然後由價值數
var _transactionsList = TransactionsData
.GroupBy(x => new { x.ItemID, x.Title, x.GalleryURL })
.Select(pr => new TransactionsTabResults
{
ItemID = pr.Key.ItemID,
Title = pr.Key.Title,
GalleryURL = pr.Key.GalleryURL,
ItemPrice = pr.OrderByDescending(a => a.TransactionDate).First().ItemPrice,
TotalSoldItems = pr.Count(),
TotalRevenuePerItem = pr.Sum(y => y.ItemPrice),
AveragePrice = pr.Average(y => y.ItemPrice),
}).ToList();
我試圖通過分組像這樣獲取的總銷售的物品價值:
ItemID Sales ItemName
1 1 Item1
1 3 Item1
1 5 Item1
1 6 Item1
2 2 Item2
2 2 Item2
2 2 Item2
2 2 Item2
所需的輸出將是:
ItemID Sales ItemName
1 15 Item1
2 8 Item2
上面我寫的查詢給了我錯誤的值總銷量說:
TotalSoldItems = pr.Count(),
我怎麼能算,或者和它有唯一的ID一個項目的所有銷售(這是我通過編組)...
我在做什麼錯? ?