I have a List<dynamic>
that I need to copy and then, based on a condition of the row of the list I need to modify a field of that row and add it to the second list.
This a sample of the code:
//list1 is a `List<dynamic>` that I get from a query using Dapper. I guess it is an ExpandoObject list
var list2 = new List<dynamic>(list1);
foreach (var obj in list2)
{
if (obj.condition == 1)
{
var newObj = obj;
newObj.description = "new row";
list2.Add(newObj);
}
}
My problem is that in both my list the obj in the list is updated with the string 'new row'.
It seems like every time I change newObj
both lists are updated.
I also tried to create my list2
this way but I have the same problem:
var list2 = new BindingList<dynamic>(list1);
EDIT:
I looked at the other questions but in my case, I only have a dynamic
List. Is it possible to get the result I want without having to create a Class
and implement ICloneable
?