开发者

C# Remove objects in one IEnumerable from another IEnumerable

开发者 https://www.devze.com 2023-03-22 10:43 出处:网络
I\'ve got a main IEnumerable collection and another smaller collection which contains some duplicates from the larger collection,

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

   IEnumerable<T> all_ob开发者_JAVA百科jects ;
   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); 
  }


all_objects = all_objects.Except(some_of_the_objects);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号