繁体   English   中英

对“复制”列表所做的更改反映了原始列表 - C#

[英]Changes made on "copy" list are reflecting original list - c#

我有两份清单,一份原件和一份复制件。 由于一个原因,我复制了原始列表:

我需要处理/工作原始列表中的数据,但我不应该编辑它。 因此,我创建了该原始列表的副本以供使用。 但不知何故,我在复制列表上所做的更改仍然会修改原始列表。

这是我的代码:

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;
        }
}

您所指的“副本列表”集合实际上并不是副本

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

你将不得不用这样的东西替换你的复制 foreach 循环:

foreach (var item in forPrintKitchen)
{
    forPrintKitchenOrders.Add(item.Clone()); // The clone method should return a new Instance after copying properties from item.
}

Clone方法应该创建new实例并从被克隆的实例中复制每个属性,然后返回新创建的实例。

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

public class OrderTemp
{
    /*  other members of the class */
    public OrderTemp Clone()
    {
        return new OrderTemp
        {
            property1 = this.property1;
            property2 = this.property2;
            /* and so on */
        };
    }
}

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

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

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

我喜欢使用 AutoMapper 来完成这个任务,但是如果你不想在你的项目中使用框架,你可以在你的OrderTemp类型和所有必要的嵌套类型上实现IClonable并调用Clone()方法。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM