Find the differences between two lists

Browsing around google it seems that you can use this:

List<string> result = list1.Except(list2).ToList();

to compare the differences between two lists. So I added my variables in and gave it a try and came up with this:

void OnEnable() {
     List<Inventory> result = playerCurrentInventoryList.Except(allInventoryList).ToList();
     CompareDifferences();
}

void CompareDifferences() {
     foreach (Inventory _item in result)
          Debug.Log ("result differences iventory" + _item.Name);
}

No debug.logs show up in the console. The playerCurrentInventoyList has a few items in it while the allInventoryList is empty. So my expected differences would be all the items in playerCurrentInventoryList.

My efforts in comparing the two lists based on the example from the google search does not work. Is anyone willing to offer some suggestions of what I am doing wrong?

thanks.

1 Answer

1

Uhm, you declare a local variable result in OnEnable, so what ever result you use in CompareDifferences is not the one in OnEnable.

You probably just want to remove the local variable declaration

Oh. Thank you very much!