时间:2019-01-14 标签:c#collection.RemoveAll(collection.OfType());

发布于 2024-12-19 07:15:59 字数 124 浏览 0 评论 0原文

我可以做这样的事情:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

挽清梦 2024-12-26 07:16:00

两个已经提交的答案都是正确的,但省略了为什么海报的示例代码不起作用的解释。有几个有趣的点:

首先,RemoveAll() 没有在 ICollectionICollection 接口中定义;例如,它是在 List 上定义的,但 HashSet 上语义上等效的方法称为 RemoveWhere()。如果您希望能够对任何 ICollection 执行此操作,则应该编写一个扩展方法。

其次,问题的示例代码传递了要从集合中删除的项目序列,但是 List.RemoveAll()HashSet.RemoveWhere()采用谓词来标识要删除的项目(如其他答案所示)。您可以编写扩展方法来采用其他方法,并传递 IEnumerable (如示例中所示)。不过,您需要小心,因为您不能这样做:

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

如果您尝试这样做,您应该收到一个 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 the ICollection or ICollection<T> interface; it's defined on List<T>, for example, but the semantically equivalent method on HashSet<T> is called RemoveWhere(). If you want to be able to do this for any ICollection<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() and HashSet<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 an IEnumerable<T> as in your example. 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 InvalidOperationException with a message like "Collection was modified; enumeration operation may not execute."

鹿童谣 2024-12-26 07:16:00

由于前两个代码答案专门使用 List,而第三个代码只有非工作代码:

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

您可以通过调用 ToList 来结束枚举,以便再次允许删除。不过,这是完全低效的,因为它需要一次枚举来获取对象,然后将它们一一删除,可能每次都需要枚举。如果您使用的集合类型有自己的方法,例如 List.RemoveAll,请改用这些方法,但您使用“集合”意味着您不知道其类型。

或者,如果重要性不在于保留对象,请考虑重新分配:

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

As the first two code answers use a List specifically, and the third only has nonworking code:

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. 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.

Alternatively, if the importance is not on preserving the object, consider reassigning instead:

collection = collection.Where(obj => obj is type == false).ToList();
幸福不弃 2024-12-26 07:16:00

如果我理解正确的话,您有一个任何对象类型的集合,并且您希望从该集合中删除该类型的所有项目。如果是这样,那就很简单了:

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

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));
枯寂 2024-12-26 07:16:00

我会做这样的事情:

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


EDIT:

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

I would do something like this:

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


EDIT:

 collection.RemoveAll(i => i is type);
傾城如夢未必闌珊 2024-12-26 07:16:00

collection.Clear() 删除所有元素。

collection.Clear() removes all elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文