2

Ok so I have List list1 that I pulled from db and I want to temporary save this to List list2, perform some operation on my list1 save it to db and then play with list2 and save it to db. The problem is that because is is passed by reference once I change some fields in list1, list2 is already updated.

Is there any way to pass whole list as a value not reference?

Here is some code to make it clearer :

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();
Adam Bielecki
  • 657
  • 2
  • 6
  • 20
  • 2
    Please post some code to show us exactly what you are doing and where it's going wrong. It is not clear exactly how deep you need this copy to be and your question is inviting speculative answers. I'll remove my downvote when this question becomes clearer. – spender Mar 25 '13 at 15:34
  • You can clone the list as shown here. http://stackoverflow.com/questions/222598/how-do-i-clone-a-generic-list-in-c – James Mar 25 '13 at 15:36
  • What kind of elements are stored in the List? – Steve Mar 25 '13 at 15:36
  • @AdamBielecki a quick google search for "deep copy" would have given you the correct answer. I would downvote but I don't have enough rep. – feralin Mar 25 '13 at 15:36

4 Answers4

4

You could try using the List<T> constructor that takes an IEnumerable<T>. Example:

List<int> originalList = new List<int>() { 0, 1, 2 };
List<int> copiedList = new List<int>(originalList);
copiedList[2] = 3; // originalList[2] still is 2
feralin
  • 3,268
  • 3
  • 21
  • 37
3

Just Another Yet Solution:

IList<string> f = new List<string>();

var t = f.ToList();
Daniil
  • 413
  • 3
  • 10
0
List<object> list2 = new List<object>(list1);

This should prevent you from referencing the other list because the values are copied.

See: http://msdn.microsoft.com/en-us/library/fkbw11z0.aspx

Caleb Keith
  • 816
  • 4
  • 10
0

I think that the given answer work only for value type. For reference type you should implement a clone method that copy all the data on a new instance of the same class Then for each element in the list you add in another list the cloned object

Fabio Marcolini
  • 2,315
  • 2
  • 24
  • 30
  • You are on the right track, now add an example and you have the right answer. – Steve Mar 25 '13 at 15:39
  • @Steve the question did not ask about copying all the items in the list; it asked only about making a list copy that does not immediately reference another list. – feralin Mar 25 '13 at 15:41