2

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?

gorkem
  • 731
  • 1
  • 10
  • 17
SilentRage47
  • 934
  • 2
  • 14
  • 31
  • Implement ICloneable and call `var list2 = list1.Select(i => i.Clone()).ToList()` probably. – Casey Jul 13 '17 at 14:03
  • but in Implement the op has to write approximately same code – Lei Yang Jul 13 '17 at 14:05
  • Well, although there are two different lists, the *elements* within those lists are the same, so modifying any of the elements in one list surley modifies it in the other one as well. – MakePeaceGreatAgain Jul 13 '17 at 14:07
  • @Casey I can't implement ICloneable since i don't have a class. I tried either way with the Select but it gives me the same amount of row but they're all null. – SilentRage47 Jul 13 '17 at 14:17
  • Which list is it? `list.Add(newObj);` – praty Jul 13 '17 at 14:23
  • @praty It's a dynamic list that I get using a query with Dapper. I updated my question. – SilentRage47 Jul 13 '17 at 14:29
  • @mjwills, @HimBromBeere: This is not a duplicate of the mentioned questions, as here we are dealing with a `List`. They don't answer the question as how `dynamic` objects can to be cloned. – Olivier Jacot-Descombes Jul 13 '17 at 14:32
  • @HimBromBeere I updated my question, is there a way to clone it without have to create a class for my object ? – SilentRage47 Jul 13 '17 at 14:43
  • No, there´s none, as actually even if your elements are of type `dynamic` they have a known runtime-type (e.g. `MyClass`). So you need a new instance of that type. You can use some reflection of course to create an instance of the runtime-type however as shown in the second answer in the duplicate. – MakePeaceGreatAgain Jul 13 '17 at 14:44
  • 1
    @SilentRage47 You want to clone the object, but you don't have access to it? Well, maybe you can use an extension method, although this might turn into messy reflection, depending on how visible or not stuff is. – Casey Jul 13 '17 at 14:50
  • I think you could edit some of the specifics of your situation into the question because I don't think this scenario is an exact duplicate of the questions linked. – Casey Jul 13 '17 at 14:51
  • 1
    Without any knowledge on your actual objects it´s impossible to clone them. See here for further explanation: https://stackoverflow.com/questions/12602152/cloning-dynamic-object-in-c-sharp – MakePeaceGreatAgain Jul 13 '17 at 19:46

1 Answers1

-1

Try

var list2 = list1.Select(x => x);
praty
  • 535
  • 2
  • 9