Changes made on “copy” list are reflecting original list - c#

后端 未结 3 395
终归单人心
终归单人心 2021-01-21 01:08

I have two lists, the original one and a copied one. I made a copy of the original list, because of one reason:

I need to process/work data from the original list, but I

3条回答
  •  盖世英雄少女心
    2021-01-21 02:02

    You basicaly create shallow copy. It means it will copy all simple types, like references and generic types, but NOT objects content.

    As solution you need to totaly copy your objects:

    foreach (var itemss in forPrintKitchen)
    {
      forPrintKitchenOrders.Add(new OrderTemp(){ /*copy itemss here*/ });
    }
    

    I love to use AutoMapper for this task, but if you don't want to take framework in you project you can just implement IClonable on your OrderTemp type and all necessary nested types and call Clone() method.

提交回复
热议问题