好的,所以我有从 db 中提取的 List list1,我想将其临时保存到 List list2,在我的 list1 上执行一些操作,将其保存到 db,然后使用 list2 并将其保存到 db。问题是,因为一旦我更改了 list1 中的某些字段,它就会通过引用传递,所以 list2 已经更新。
有没有办法将整个列表作为值而不是参考传递?
这里有一些代码让它更清楚:
var CurrentMenuItems = dbContext.menu_items.Where(m => m.optgroup == currentGroup && m.day_id == menuItem.day_id).ToList();
List<menu_item> MenuItemsToBeEditedAfterSubmitChanges = CurrentMenuItems;// we need to store this by value, so we can update it later
byte ItemIncrease = 50; // To store temp value so we can avoid duplicate entry for item number
foreach (var item in CurrentMenuItems)
{
item.optgroup = nextGroup;
item.item = ItemIncrease;
ItemIncrease++;
}
var menuItemsToBeReplaced = dbContext.menu_items.Where(m => m.optgroup == nextGroup && m.day_id == menuItem.day_id).ToList(); // we want to move all items within this group
dbContext.SubmitChanges();
foreach (var item in menuItemsToBeReplaced)
{
item.optgroup = currentGroup;
item.item = (byte)(item.item - 1);
}
dbContext.SubmitChanges();
// After first save, update the first group of menu items
foreach (var item in MenuItemsToBeEditedAfterSubmitChanges)
{
item.optgroup = nextGroup;
item.item = (byte)(item.item + 1);
}
dbContext.SubmitChanges();