对"复制"所做的更改列表反映原始列表 - c#

时间:2016-09-22 07:41:57

标签: c# list

我有两个列表,原始列表和复制列表。我制作了原始列表的副本,原因有一个:

我需要处理/处理原始列表中的数据,但我不应该编辑它。所以我创建了一份原始列表的副本。但是我在复制列表上做的某些改变仍然会修改原始文件。

这是我的代码:

forPrintKitchenOrders = new List<OrderTemp>();
foreach (var itemss in forPrintKitchen)
{
  forPrintKitchenOrders.Add(itemss); // HERE I AM ADDING ITEMS TO ANOTHER LIST BECAUSE I DON'T WANT TO EDIT ORIGINAL LIST (Change quantity etc)
}

if (forPrintKitchen.Count > 0)
{

  foreach (var item in forPrintKitchenOrders.ToList())
  {
      foreach (var item2 in mainList)
      {
          if (item2.MainProductID == Convert.ToInt32(item._articleCode))
          {
              //I don't know why this is happening. I loop another list (copy of original list, because I didn't want to harm original list), and when I find certain item I am reducing quantity (-1),
              //And later I realized and saw while I was debugging, that the value of quantity in my original "forPrintKitchen" list is also edited, I don't know how changed reflected there..

              int calculate = Convert.ToInt32(item._quantity)-1; //this block is making me trouble, here I am reducing quantity and later that reflects to my forPrintKitchen list even if I am editing and looping //forPrintKitchenOrders(original's copy)
              item._quantity = calculate.ToString();
          }
      }
  }
    foreach (var items in forPrintKitchen) //THIS IS MY ORIGILAN LIST AND SHE SHOULD NOT BE EDITED WHEN I EDIT "forPrintKitchenOrders" item
    {
    //Original List
        OrdersKitchen kitchen = new OrdersKitchen();
        kitchen.ProductID = Convert.ToInt32(items._articleCode);
        kitchen.UserID = Util.User.UserID;
        kitchen.UserName = Util.User.FirstName
        kitchen.LastName = Util.User.LastName
        kitchen.BillID = bill.BillID;
        kitchen.Quantity = Convert.ToInt32(items._Quantity);
        OrdersKitchen.Add(kitchen);
    }
    foreach (var itemss in mainList)
    {

        OrdersKitchen kitchen2 = new OrdersKitchen();
        kitchen2.ProductID = Convert.ToInt32(itemss.MainProductID);
        kitchen2.UserID = User.UserID;
        kitchen2.UserName = Util.User.FirstName;
        kitchen2.LastName = Util.User.LastName;
        kitchen2.BillID = bill.BillID;
        kitchen2.Quantity = Convert.ToInt32(0); //HARDCODE ZERO
        OrdersKitchen.Add(kitchen2);
    }
 }

mainList.Clear();
//forPrintKitchenOrders.Clear();
}

在看到回复之后,我读了@ sachin的帖子并写了一个与他们类似的代码。这好吗?看起来它现在正在工作,但我不确定这个解决方案是否正常?

foreach (var itemss in forPrintKitchenOrders)
{
    forPrintKitchenOrders.Add(new OrderTemp(itemss._articleCode,itemss._name,itemss._quantity,itemss._amount));
}

public class OrderTemp
{
        public string _articleCode;
        public string _name;
        public string _quantity;
        public double _amount;

        public OrderTemp(string articleCode, string name, string quantity, double amount)
        {
            _articleCode = amount;
            _name = name;
            _quantity = quantity;
            _amount = amount;
        }
}

3 个答案:

答案 0 :(得分:2)

您所指的集合为&#39; 复制列表&#39;是实际上不是副本

Collection中的元素引用与原始集合中相同的对象

您必须使用以下内容替换复制的foreach循环:

std::cout << new_edge_pm[new_mesh.edge_handle(23)] << std::endl;
// Should print "foo"

foreach (var item in forPrintKitchen) { forPrintKitchenOrders.Add(item.Clone()); // The clone method should return a new Instance after copying properties from item. } 方法应该创建Clone实例并从正在克隆的实例中复制每个属性,然后返回新创建的实例。

基本上,您必须new类中定义一个名为Clone(名称无关紧要)的方法,如下所示:

OrderTemp

答案 1 :(得分:0)

您基本上创建副本。这意味着它将复制所有简单类型,如引用和泛型类型,但 NOT 对象内容。

作为解决方案,您需要完全复制对象:

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

我喜欢使用AutoMapper执行此任务,但如果您不想在项目中使用框架,则可以在 OrderTemp 类型上实现 IClonable 必要的嵌套类型并调用 Clone()方法。

答案 2 :(得分:0)

您制作了列表的副本,但副本仍包含对相同对象的引用;对象本身不会被复制。