I am encountering a rather strange problem. I am iterating over a type of List and within my loop I assign the iterator variable to another local object. Now changing anything in that local object is causing changes in the List on which loop is iterating. Let me clear this with a code sample.
var balances = DBHelperADO.Select("select * from Orders");
// balances is of type List<MyModel>
foreach (var item in balances)
{
MyModel model = new MyModel();
model = item;
var thisQty = details.Where(x => x.Code == item.Code).Sum(x => x.QTY);
// details is another List<MyModel> holding values from the GUI
model.BLNC = model.BLNC - thisQty;
model.VAL = (model.BLNC == 0) ? 0 : model.VAL - (thisQty * model.RATE);
model.TABLE = "Orders";
toUpdate.Add(model); // toUpdate is a List<MyModel>
}
Now my understanding is that the iterator variable (in this case the item) is readonly and when I write:
model = item;
I am making a copy of my item values. But when I do the the calculations on the model object it causes the same changes on the balances list.
I am not getting why its effecting the balances list. I am performing calculations on local scoped model object. Then why those are reflecting on the iterating list (balances).
Please help me what I am doing wrong.
Regards