tags:

views:

100

answers:

3

how to remove content of list from anotherlist ?

+8  A: 
list1.RemoveAll(i => list2.Contains(i));
James
+1 altho the names `myList` and `myOtherList` are misleading in the context, as they are the exact opposite of the requested "remove ... from anotherlist"
David Hedlund
@David, good point I will change them
James
Does this remove objects that are the same object, or object that equal each other (o1.Equals(o2))?
SeaDrive
@SeaDrive: See http://msdn.microsoft.com/en-us/library/bhkz42b3(VS.80).aspx
James
A: 

Here is a short addition and advise to James post.

If you are using List<T> and the myOtherList contains many items you should convert it to a Hashset<T> var set = new Hashset(myOtherList), so his solution should run much faster.

DHN
A: 
List<object> result = anotherlist.Except(list).ToList();
VMAtm