I have two lists. One filled with data from a database table, and the other is empty. I want to add the objects from orderList
to VM.ViewOrders based on an object attribute called ProductName. If an object in VM.ViewOrder already contains the ProductName if it should not be added to the list. Instead, add one to the attribute numOfProd
.
Any help would be appreciated!
I tried using foreach
, but you cant modify the iterated list. I tried making a copy of the VM.ViewOrderList, but it gives me a: "System.OutOfMemoryException".
Also tried to add index 0 of orderList to vm.ViewOrder,
List <Order> orderList = db.Orders.ToList();
vm.ViewOrders = new List<Order>();
foreach(Order order in orderList) {
foreach(Order order1 in vm.ViewOrder)) {
if (order.ProductName.Equals(order1.ProductName)) {
order1.numOfProd++;
}
else {
vm.ViewOrder.Add(order);
order.numOfProd = 1;
}
}
}
System.OutOfMemoryException
Collection was modified; enumeration operation may not execute.