Removing objects contained in a List from another List?

IEnumerable. Except is your friend (if I'm reading your example correctly): allElements = allElements. Except(searchResults).

You can use the Enumerable. Except Method (from MSDN) List FilteredElements = AllElements. Except(SearchResults).ToList().

You'll need to tag a ToList call on the end if you want the result to be a List rather than IEnumerable. – LukeH Aug 18 '10 at 21:00.

The following will give you a 'list' where everything in SearchResults was removed. Var results = AllElements. Where(i =>!SearchResults.

Contains(i)); You could then do: AllElements = results.AsList().

– Justin Niessner Aug 18 '10 at 18:42 1 @Justin, the benefit for me was that I knew about the Where() way to do it. I had no idea you could use Except(). I posted my answer at the same time as your, and I'm gonna leave it up because it is right, and it is good to know other ways to do things.

– Justin 'jjnguy' Nelson Aug 18 '10 at 18:57 3 (s): There is a semantic difference between them as well. Except produces the set difference of the two sequences which means that any duplicates are also removed. For example, if the two sequences are { 1, 2, 3, 5, 1, 2, 4 } and { 3, 4 } then using Except will give you the sequence { 1, 2, 5 } whereas using Where/Contains will give you { 1, 2, 5, 1, 2 }.

– LukeH Aug 18 '10 at 21:10 @Luke, interesting, thanks for the info. – Justin 'jjnguy' Nelson Aug 18 '10 at 21:24.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions