简体   繁体   English

c#collection.RemoveAll(collection.OfType <type> ());

[英]c# collection.RemoveAll(collection.OfType<type>());

Can I do something like this: 我可以这样做:

collection.RemoveAll(collection.OfType<type>());

To remove all the elements of a given type from a collection? 要从集合中删除给定类型的所有元素?

Both of the already-submitted answers are correct, but omit an explanation why the poster's sample code doesn't work. 两个已经提交的答案都是正确的,但省略了解释为什么海报的示例代码不起作用。 A couple of interesting points: 一些有趣的观点:

First, RemoveAll() is not defined in the ICollection or ICollection<T> interface; 首先,未在ICollectionICollection<T>接口中定义RemoveAll() ; it's defined on List<T> , for example, but the semantically equivalent method on HashSet<T> is called RemoveWhere() . 例如,它在List<T>上定义,但HashSet<T>上的语义等效方法称为RemoveWhere() If you want to be able to do this for any ICollection<T> , you should write an extension method. 如果您希望能够为任何ICollection<T>执行此操作,则应编写扩展方法。

Second, the question's sample code passes a sequence of items to be removed from the collection, but List<T>.RemoveAll() and HashSet<T>.RemoveWhere() take a predicate to identify the items to be removed (as shown in the other answers). 其次,问题的示例代码传递要从集合中删除的一系列项,但List<T>.RemoveAll()HashSet<T>.RemoveWhere()采用谓词来标识要删除的项(如图所示)其他答案)。 You could write your extension method to take the other approach, and pass an IEnumerable<T> as in your example. 您可以编写扩展方法以采用其他方法,并在示例中传递IEnumerable<T> You need to be careful, though, because you can't do this: 但是,你需要小心,因为你不能这样做:

foreach (var item in collection)
    if (ShouldRemove(item))
        collection.Remove(item);

If you try to do that, you should get an InvalidInvalidOperationException with a message like "Collection was modified; enumeration operation may not execute." 如果您尝试这样做,您应该得到一个InvalidInvalidOperationException,其中包含“收集已被修改;枚举操作可能无法执行”之类的消息。

If I understand it correctly, you have a single collection of any object type and you want to remove all items of a type from that collection. 如果我理解正确,您可以拥有任何对象类型的单个集合,并且您希望从该集合中删除所有类型的项目。 If so, it's simple: 如果是这样,那很简单:

objects.RemoveAll(q=>q.GetType()==typeof(YourType));

As the first two code answers use a List specifically, and the third only has nonworking code: 由于前两个代码答案专门使用List,而第三个只有非工作代码:

ICollection collection = GetCollection();
foreach (object obj in collection.Where(obj => obj is type).ToList())
     collection.Remove(obj);

You can end the enumeration by calling ToList , so that removal is allowed again. 您可以通过调用ToList来结束枚举,以便再次允许删除。 It's totally inefficient tho, as it requires one enumeration to get the objects and then removes them one by one, possibly requiring enumeration each time. 它完全没有效率,因为它需要一个枚举来获取对象然后逐个删除它们,每次都可能需要枚举。 If the collection type you're using has it's own methods, like List.RemoveAll , use those instead, but your use of "collection" implies you do not know its type. 如果你正在使用的集合类型有它自己的方法,比如List.RemoveAll ,请改用它们,但是你使用“集合”意味着你不知道它的类型。

Alternatively, if the importance is not on preserving the object, consider reassigning instead: 或者,如果重要性不是保留对象,请考虑重新分配:

collection = collection.Where(obj => obj is type == false).ToList();

I would do something like this: 我会做这样的事情:

collection.RemoveAll(i => collection.OfType().Contains(i)); collection.RemoveAll(i => collection.OfType()。Contains(i));


EDIT: 编辑:

  collection.RemoveAll(i => i is type); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM