我试图按2个字段(类别,然后是供应商)对列表进行分组,但是值不同.如果类别为"01",则将所有成本加起来.如果类别不是"01",则按类别分组,然后按供应商分组.
一些演示数据:
List<MyItem> myItemList = new List<MyItem>();
myItemList.Add(new MyItem{Vendor="Ven1", Cost=100, Category="01"});
myItemList.Add(new MyItem{Vendor="Ven2", Cost=10, Category="02"});
myItemList.Add(new MyItem{Vendor="Ven3", Cost=50, Category="02"}));
myItemList.Add(new MyItem{Vendor="Ven2", Cost=40, Category="01"});
myItemList.Add(new MyItem{Vendor="Ven2", Cost=20, Category="01"});
myItemList.Add(new MyItem{Vendor="Ven3", Cost=30, Category="02"});
myItemList.Add(new MyItem{Vendor="Ven1", Cost=10, Category="03"});
Run Code Online (Sandbox Code Playgroud)
我目前在做什么:
List<MyItem> groupedItems = myItemList.GroupBy(a=> new {a.Category, a.Vendor})
.Select(b=> new MyItem{
Vendor = b.First().Vendor,
Cost = b.Sum(c => c.Cost),
Category = b.First().Category
}).ToList();
Run Code Online (Sandbox Code Playgroud)
我想做什么(也就是我最好的猜测):
List<MyItem> groupedItems = myItemList.GroupBy(a=> new {a.Category.Where(z=>z.Category.Equals("01:)), a.Vendor})
.Select(b=> new MyItem{
Vendor = b.First().Vendor,
Cost = b.Sum(c => c.Cost),
Category = b.First().Category …Run Code Online (Sandbox Code Playgroud)