Adrian
Adrian

Reputation: 20058

Remove a list of items from another list

Lets say I have a listBox with 10 elements, and I put the first 4 elements in a List list1, and the rest of elements in another List list2:

list2 = listbox.remove(list1);

Something like this. Is this posible ?

Thanks.

Upvotes: 1

Views: 162

Answers (1)

Jon
Jon

Reputation: 437336

You can do this easily with LINQ:

list2 = listbox.Items.Except(list1);

However, Except needs to compare the items in list and the items in listbox to produce its results. The above example (default behavior) assumes that the type of the items in the lists are uniquely identified by a reference comparison (which sounds fine in this case).

If the class implements IEquatable<T> and/or overrides Equals, then Except uses that method to test for equality.

Upvotes: 5

Related Questions