Reputation: 3366
{
"ProductDMList":
[
{
"ProductID" : 1,
"CabinetList":
[
{
"Min" : 1,
"Max" : 12
}
]
},
{
"ProductID" : 1,
"CabinetList":
[
{
"Min" : 16,
"Max" : 100
}
]
}
]
}
I am using the below code to generate the above list.
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; }
}
How can I join the above 2 lists using ProductID
.
Ex : If the ProductID
is same I want to create one list and bind all cabiletLists
inside it.
Expected Output
{
"ProductDMList":
[
{
"ProductID" : 1,
"CabinetList":
[
{
"Min" : 1,
"Max" : 12
},
{
"Min" : 16,
"Max" : 100
}
]
}
]
}
I tried AddRange()
and Concat()
methods. But I was unable to get the above expected result.
Upvotes: 0
Views: 76
Reputation: 1
Javascript solution so easy :)
var tempaa = {
"ProductDMList":
[
{
"ProductID": 1,
"CabinetList":
[
{
"Min": 1,
"Max": 12
}
]
},
{
"ProductID": 1,
"CabinetList":
[
{
"Min": 16,
"Max": 100
}
]
}
]
};
var tempbb = [];
function isExistsInBB(productId, currentProductDM, sourceCabinetList) {
if (tempbb.length == 0) {
tempbb.push(currentProductDM);
return;
}
for (var i = 0; i < tempbb.length; i++) {
var innerItem = tempbb[i];
if (productId == innerItem.ProductID) {
innerItem.CabinetList.push(sourceCabinetList);
} else {
tempbb.push(currentProductDM);
}
}
}
function eachTempaa() {
for (var i = 0; i < tempaa.ProductDMList.length; i++) {
var innerItem = tempaa.ProductDMList[i];
isExistsInBB(innerItem.ProductID, innerItem, innerItem.CabinetList[0]);
}
console.log(tempbb);
}
eachTempaa();
Upvotes: 0
Reputation: 81513
Maybe this? If i understand what you are asking
var list = new List<ProductDM>();
var result = list.GroupBy(x => x.ProductID)
.Select(x => new ProductDM
{
ProductID = x.Key,
Min = x.Min(y => y.Min),
Max = x.Max(y => y.Max),
CabinetList = x.SelectMany(y => y.CabinetList).ToList()
}).ToList();
Enumerable.GroupBy Method (IEnumerable, Func)
Groups the elements of a sequence according to a specified key selector function.
Upvotes: 1
Reputation: 1084
I would propose to use a Dictionary to access already seen Products by their ID and then add the InventoryDM instances as you loop over the un-merged List:
static void Main(string[] args)
{
List<ProductDM> productDMList = new List<ProductDM>()
{
new ProductDM()
{
ProductID = 1,
CabinetList = new List<InventoryDM>()
{
new InventoryDM()
{
Min = 1,
Max = 12
}
}
},
new ProductDM()
{
ProductID = 1,
CabinetList = new List<InventoryDM>()
{
new InventoryDM()
{
Min = 16,
Max = 100
}
}
},
};
Dictionary<int, ProductDM> dict = new Dictionary<int, ProductDM>();
foreach(ProductDM product in productDMList)
{
if(!dict.ContainsKey(product.ProductID))
{
dict.Add(product.ProductID, product);
}
else
{
dict[product.ProductID].CabinetList.AddRange(product.CabinetList.ToArray());
}
}
Console.ReadKey(true);
}
dict.Values
is then your merged List
Upvotes: 1