在“副本"上所做的更改列表反映了原始列表-C# [英] Changes made on "copy" list are reflecting original list - c#
问题描述
我有两个列表,原始列表和副本列表.由于以下原因,我制作了一份原始清单的副本:
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 shouldn't edit it. So I created a copy of that original list to work with. But somehow changes I do on the copied list still modifies the original.
这是我的代码:
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的帖子,并编写了与他们相似的代码.这样好吗看来它现在正在运行,但是我不确定该解决方案还可以吗?
After I saw the replies, I read @sachin's post and wrote a code similar to theirs. Is this alright? It looks like it's working right now, but I am not sure is this solution ok?
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;
}
}
推荐答案
您称为"副本列表"的集合实际上不是副本.
The collection that you are referring to as 'Copy List' is not actually a Copy.
集合中的元素是指与原始集合中相同的对象.
您将必须用以下代码替换foreach循环:
You will have to replace your copying foreach loop with something like this:
foreach (var item in forPrintKitchen)
{
forPrintKitchenOrders.Add(item.Clone()); // The clone method should return a new Instance after copying properties from item.
}
Clone
方法应创建new
实例,并从要克隆的实例中复制每个属性,然后返回新创建的实例.
The Clone
method should create new
Instance and copy each of the properties from the instance being cloned and then return the newly created Instance.
基本上,您必须像这样在OrderTemp
类中定义一个名为Clone(名称无关紧要)的方法:
Basically you'll have to define a method named Clone (name doesn't matter) in OrderTemp
class like this:
public class OrderTemp
{
/* other members of the class */
public OrderTemp Clone()
{
return new OrderTemp
{
property1 = this.property1;
property2 = this.property2;
/* and so on */
};
}
}
这篇关于在“副本"上所做的更改列表反映了原始列表-C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!