簡體   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