C#分组求和

List<ChemicalInventory> listInventory = new List<ChemicalInventory>();

foreach(

ChemicalInventory Inventory = new ChemicalInventory();

//赋值省略

listInventory.Add(Inventory);

)

//手动添加一个相同编号相同区域的数据测试一下
ChemicalInventory Inventory2 = new ChemicalInventory();
Inventory2.infocode = "000000008000000001";
Inventory2.regioncode = "1102";//对应工智道设施分区编码
Inventory2.number = 100;//对应工智道设施分区编码
listInventory.Add(Inventory2);

var groupList = listInventory.GroupBy(m => new { m.infocode, m.regioncode }).
Select(a => new
{
infocode = a.Key.infocode,
regioncode = a.Key.regioncode,
number = a.Sum(c => c.number)
}).ToList();

posted @ 2022-11-07 23:52  半、枫  阅读(825)  评论(0)    收藏  举报