简体   繁体   English

从IEnumerable <T>中删除项目

[英]Remove items from IEnumerable<T>

I have 2 IEnumerable collections. 我有2个IEnumerable集合。

IEnumerable<MyClass> objectsToExcept 

and

IEnumerable<MyClass> allObjects.

objectsToExcept may contain objects from allObjects . objectsToExcept可以含有从物体allObjects

I need to remove from allObjects objects in objectsToExcept . 我需要从allObjects对象中objectsToExcept For example: 例如:

foreach (var myClass in objectsToExcept)
{
allObjects.Remove(myClass);
}

Or 要么

allObject.Except(objectsToExcept)

But it doesn't work. 但它不起作用。 The count after the methods have execute indicate that no items have been removed. 方法执行后的计数表示没有删除任何项目。

I don't see how the first version would compile, and the second version won't do anything unless you use the result. 我没有看到第一个版本如何编译,第二个版本将不会做任何事情,除非你使用结果。 It doesn't remove anything from the existing collection - indeed, there may not even be an in-memory collection backing it. 它不会从现有集合中删除任何东西-的确,甚至可能不是一个内存中的集合支持它。 It just returns a sequence which, when iterated over, will return the appropriate values. 它只返回一个序列,当迭代时,它将返回适当的值。

If you are using the result, eg 如果您正在使用结果,例如

IEnumerable<MyClass> others = allObjects.Except(objectsToExcept);
foreach (MyClass x in others)
{
    ...
}

then it should be fine if you've overridden GetHashCode and Equals or if you're happy to use reference equality. 如果你已经覆盖了GetHashCodeEquals 或者你很乐意使用引用相等,那么应该没问题。 Are you trying to remove logically-equal values, or do the same references occur in both sequences? 您是否尝试删除逻辑上相等的值,或者两个序列中是否都出现相同的引用 Have you overridden GetHashCode and Equals , and if so, are you sure those implementations work? 你有没有覆盖GetHashCodeEquals ,如果有的话,你确定这些实现有效吗?

Basically it should be fine - I suggest you try to create a short but complete program that demonstrates the problem; 基本上它应该没问题 - 我建议你尝试创建一个简短但完整的程序来演示问题; I suspect that while doing so, you'll find out what's wrong. 我怀疑这样做,你会发现什么是错的。

There is no Remove method on IEnumerable<T> , because it is not meant to be modifiable. IEnumerable<T>上没有Remove方法,因为它不是可修改的。

The Except method doesn't modify the original collection: it returns a new collection that doesn't contain the excluded items: Except方法不会修改原始集合:它返回一个不包含排除项的新集合:

var notExcluded = allObjects.Except(objectsToExcept);

See Except on MSDN . 请参阅MSDN上的“ Except ”。

Remove and Except do not modify the original IEnumerable. 删除和除外不要修改原始的IEnumerable。 They return a new one. 他们返回一个新的。 try 尝试

var result = allObject.Except(objectsToexcept);

While the other answers are correct, I have to add that the result of the Except() call can be assigned back to the original variable. 虽然其他答案是正确的,但我必须补充说,可以将Except()调用的结果分配回原始变量。 That is, 那是,

allObjects = allObjects.Except(objectsToExcept);

Also keep in mind that Except() will produce the set difference of the two collections, so if there are duplicates of the variables to be removed, they will all be removed. 还要记住, Except()将产生两个集合的集合差异,因此如果要删除的变量重复,则将全部删除它们。

others.Where(contract => !objectsToExcept.Any()).ToList();

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

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