所以我有一类产品:
class Product{
public int id;
public int price;
public string product_name;
}
var BoughtProductList = new List<Product>();
BoughtProductList .Add(new Product() { id = 1, price = 3 product_name = "orange" });
BoughtProductList .Add(new Product() { id = 2, price = 2 product_name = "apple" });
BoughtProductList .Add(new Product() { id = 2, price = 2 product_name = "apple" });
BoughtProductList .Add(new Product() { id = 3, price = 3 product_name = "banana" });
Run Code Online (Sandbox Code Playgroud)
现在我想把它放在一张桌子上,这样它就会显示我买的所有产品,但我不想再看两次苹果产品.如何在linq中查看该列表并且不创建该类的新实例并添加相同的产品ID以查看我在该列表中销售该苹果的金额.
from produce in BoughtProductList
group produce.price by new {produce.id, produce.product_name} into grp
select new{grp.id, grp.product_name, TotalPrice = grp.Sum()};
Run Code Online (Sandbox Code Playgroud)