时间:2019-01-14 标签:c#collection.RemoveAll(collection.OfType());
我可以做这样的事情:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
两个已经提交的答案都是正确的,但省略了为什么海报的示例代码不起作用的解释。有几个有趣的点:
首先,
RemoveAll()
没有在ICollection
或ICollection
接口中定义;例如,它是在List
上定义的,但HashSet
上语义上等效的方法称为RemoveWhere()
。如果您希望能够对任何ICollection
执行此操作,则应该编写一个扩展方法。其次,问题的示例代码传递了要从集合中删除的项目序列,但是
List.RemoveAll()
和HashSet.RemoveWhere()
采用谓词来标识要删除的项目(如其他答案所示)。您可以编写扩展方法来采用其他方法,并传递IEnumerable
(如示例中所示)。不过,您需要小心,因为您不能这样做:如果您尝试这样做,您应该收到一个
InvalidOperationException
,其中包含类似“集合已修改;枚举操作可能无法执行”的消息。 ”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 theICollection
orICollection<T>
interface; it's defined onList<T>
, for example, but the semantically equivalent method onHashSet<T>
is calledRemoveWhere()
. If you want to be able to do this for anyICollection<T>
, you should write an extension method.Second, the question's sample code passes a sequence of items to be removed from the collection, but
List<T>.RemoveAll()
andHashSet<T>.RemoveWhere()
take a predicate to identify the items to be removed (as shown in the other answers). You could write your extension method to take the other approach, and pass anIEnumerable<T>
as in your example. You need to be careful, though, because you can't do this:If you try to do that, you should get an
InvalidOperationException
with a message like "Collection was modified; enumeration operation may not execute."由于前两个代码答案专门使用 List,而第三个代码只有非工作代码:
您可以通过调用
ToList
来结束枚举,以便再次允许删除。不过,这是完全低效的,因为它需要一次枚举来获取对象,然后将它们一一删除,可能每次都需要枚举。如果您使用的集合类型有自己的方法,例如List.RemoveAll
,请改用这些方法,但您使用“集合”意味着您不知道其类型。或者,如果重要性不在于保留对象,请考虑重新分配:
As the first two code answers use a List specifically, and the third only has nonworking code:
You can end the enumeration by calling
ToList
, so that removal is allowed again. 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, likeList.RemoveAll
, use those instead, but your use of "collection" implies you do not know its type.Alternatively, if the importance is not on preserving the object, consider reassigning instead:
如果我理解正确的话,您有一个任何对象类型的集合,并且您希望从该集合中删除该类型的所有项目。如果是这样,那就很简单了:
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:
我会做这样的事情:collection.RemoveAll(i => collection.OfType().Contains(i));EDIT:
I would do something like this:collection.RemoveAll(i => collection.OfType().Contains(i));EDIT:
collection.Clear()
删除所有元素。collection.Clear()
removes all elements.