Home > database >  How to swap values from one generic list to another
How to swap values from one generic list to another

Time:11-11

I have 2 T Lists e.g: list1 with attributes {ID, Name, Email} list2 is of the same type What I want is to be able to replace list1 ID values with ID values from list2, not affecting any Name and Email values Eventually I would be even happier if I could have List list2 values to replace ID values from list1. Thanks a lot.

I have tryed using for loop but it does not seem to work:

`for (int i = 0; i < list1.Count; i  )
            {
                list1[i].GroupID =list2[i].GroupID;
            }`

CodePudding user response:

for (var i = 0; i < list1.Count; i  )
{
   var temp = list1[i].GroupID;
   // swap
   list1[i].GroupID = list2[i].GroupID;
   list2[i].GroupID = temp;
}

Or Use tuple.

for (var i = 0; i < list1.Count; i  )
{
   // swap
   (list1[i].GroupID, list2[i].GroupID) = (list2[i].GroupID, list1[i].GroupID)
}

Both methods must have the same count of list1 and list2.

  • Related