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

Rox*_*Pro 4 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();
}
Run Code Online (Sandbox Code Playgroud)

看到回复后,我阅读了@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;
        }
}
Run Code Online (Sandbox Code Playgroud)

sac*_*hin 5

您所说的“复制列表”集合实际上并不是复制

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

您必须将复制的 foreach 循环替换为如下内容:

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

Clone方法应创建newInstance 并从正在克隆的实例复制每个属性,然后返回新创建的 Instance。

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

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