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
  • 5,211
  • 2
  • 33
  • 60
eran otzap
  • 12,293
  • 20
  • 84
  • 139
  • 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
  • 14,287
  • 6
  • 48
  • 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
  • 3,104
  • 1
  • 32
  • 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