我需要在订单列表中获取唯一商品的数量。
我有一个订单列表,每个订单都有一个项目列表。
public class Order
{
public int orderID;
public List<items> itemList;
}
public class Item
{
public int itemID;
public int itemQuantity;
public String itemDescription;
}
我的最终目标是一系列唯一商品(基于itemID或itemDescription),以及每件商品的总数。
到目前为止我有以下内容,但我不确定下一步该做什么:
var x = from order in orders
from items in order.OrderItems
group // I know I need to group them here
select new { // I have to count in here };
我需要结果如下:
对此有任何帮助或指导都会很棒。感谢。
答案 0 :(得分:2)
这将使用ItemID和ItemDescription值进行总结:
var x = from order in orders
from item in order.OrderItems
group item by new { ItemID = item.itemID, ItemDescription = item.itemDescription } into g
select new { ItemID = g.Key.ItemID, ItemDescription = g.Key.ItemDescription, Count = g.Sum(o => o.itemQuantity) };
答案 1 :(得分:0)
Orders.SelectMany(x => x.itemList)
.GroupBy(x => x.itemID)
.Select(x => new { itemID = x.Key, Count = x.Count() })
.ToList()