Search code examples
c#collectionsienumerable

C# Remove objects in one IEnumerable from another IEnumerable


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); 
  }

Solution

  • all_objects = all_objects.Except(some_of_the_objects);