【发布时间】:2018-05-30 07:50:12
【问题描述】:
{
"ProductDMList":
[
{
"ProductID" : 1,
"CabinetList":
[
{
"Min" : 1,
"Max" : 12
}
]
},
{
"ProductID" : 1,
"CabinetList":
[
{
"Min" : 16,
"Max" : 100
}
]
}
]
}
我正在使用下面的代码来生成上面的列表。
List<ProductDM> productDMList = _orderRepo.GetSuggestedList(23, 12);
for (int i=0;i<productDMList.Count;i++)
{
productDMList[i].CabinetList.Add(new InventoryDM {
Min = productDMList[i].Min,
Max = productDMList[i].Max
});
}
public class ProductDM
{
public List<InventoryDM> CabinetList { get; set; }
public int ProductID { get; set; }
public double Min { get; set; }
public double Max { get; set; }
}
public class InventoryDM
{
public Double Min { get; set; }
public Double Max { get; set; }
}
如何使用ProductID 加入上述两个列表。
例如:如果ProductID 相同,我想创建一个列表并将所有cabiletLists 绑定在其中。
预期输出
{
"ProductDMList":
[
{
"ProductID" : 1,
"CabinetList":
[
{
"Min" : 1,
"Max" : 12
},
{
"Min" : 16,
"Max" : 100
}
]
}
]
}
我尝试了AddRange() 和Concat() 方法。但是我无法得到上述预期的结果。
【问题讨论】:
-
GroupBy是你的朋友 -
你需要 10000 rep 才能看到它,这是一个链接到最后一个被删除的问题
-
已经问过:stackoverflow.com/questions/24579324/…希望这会有所帮助。
-
@TheGeneral:OP 显然没有 10k 代表。