List<ChemicalInventory> listInventory = new List<ChemicalInventory>();
foreach (var item in response.EX_TAB)
{
ChemicalInventory Inventory = new ChemicalInventory();
Inventory.infocode = item.MATNR.ToString();//物料编号
Inventory.regioncode = item.LGORT.ToString();//对应工智道设施分区编码
Inventory.number = Convert.ToDouble(item.CLABS.ToString());//对应工智道设施分区编码
string matnr = item.MATNR.ToString();//物料编号
string lgort = item.LGORT.ToString();//对应工智道设施分区编码
string clabs = item.CLABS.ToString();//对应工智道数量
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();
JsonSerializer serializer = new JsonSerializer();
StringWriter sw = new StringWriter();
serializer.Serialize(new JsonTextWriter(sw), groupList);
json = sw.GetStringBuilder().ToString();
标签:聚合,infocode,linq,工智道,item,ToString,Inventory,new,分组 From: https://www.cnblogs.com/banfeng/p/17242074.html