0

I've got a main IEnumerable collection and another smaller collection which contains some duplicates from the larger collection,

   IEnumerable<T> all_objects ;
   IEnumerable<T> some_of_the_objects ;

I'm looking for a "better looking" way to remove all the objects from some_of_the_objects from all_objects , without having to loop through the smaller collection.

  foreach(T _object in some_of_the_objects)
  {
      all_objects.Remove(_object); 
  }
Josh Gallagher
  • 4,954
  • 2
  • 32
  • 54
eran otzap
  • 11,845
  • 20
  • 77
  • 133
  • on a related matter here's an answer about joining tow lists with out duplicate values http://stackoverflow.com/questions/4031262/how-to-merge-2-listt-with-removing-duplicate-values-in-c – eran otzap Jul 26 '11 at 23:32

2 Answers2

8
all_objects = all_objects.Except(some_of_the_objects);
Ivan Danilov
  • 13,696
  • 5
  • 45
  • 66
1

Ivan's answer is on the right track; first you might need a home grown equality comparer unless a duplicate is literally another reference to the same object. But if you have a unit of uniqueness (ID, name, some combination of properties), you could pass that func as a predicate to .Except and have the list de-duplicated as you wish.

Paul Smith
  • 2,905
  • 1
  • 29
  • 45
  • the records are references to the same objects, so Ivan's answer seems to work. thanks for the info though. – eran otzap Jul 25 '11 at 04:40