重复的 IEnumerable、列表和强制转换
阅读此关于重复删除的非常有趣的线程后,我结束了有了这个=>
public static IEnumerable<T> deDuplicateCollection<T>(IEnumerable<T> input)
{
var hs = new HashSet<T>();
foreach (T t in input)
if (hs.Add(t))
yield return t;
}
顺便说一句,由于我是 C# 新手并且来自 Python,所以我在强制转换和此类事情之间有点迷失...我能够使用 : 进行编译和构建,
foreach (KeyValuePair<long, List<string>> kvp in d)
{
d[kvp.Key] = (List<string>) deDuplicateCollection(kvp.Value);
}
但我一定在这里错过了一些东西。 ..当我在运行时收到“System.InvalidCastException”时,也许你能指出有关强制转换的有趣的事情以及我错在哪里?先感谢您。
after reading this very interesting thread on duplicate removal, i ended with this =>
public static IEnumerable<T> deDuplicateCollection<T>(IEnumerable<T> input)
{
var hs = new HashSet<T>();
foreach (T t in input)
if (hs.Add(t))
yield return t;
}
by the way, as i'm brand new to C# and coming from Python, i'm a bit lost between casting and this kind of thing... i was able to compile and build with :
foreach (KeyValuePair<long, List<string>> kvp in d)
{
d[kvp.Key] = (List<string>) deDuplicateCollection(kvp.Value);
}
but i must have missed something here... as i get a "System.InvalidCastException" @ runtime, maybe could you point interesting things about casting and where i'm wrong? Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,关于方法的使用。
放弃强制转换,对方法的结果调用
ToList()
。该方法的结果是IEnumerable
,这不是List
。源最初是一个List
这一事实是无关紧要的,您不返回列表,您产生返回
一个序列。其次,您的 deDuplicateCollection 方法是多余的,Distinct() 已经存在于库中并执行相同的功能。
只需确保指令中有
using System.Linq;
即可使用这些Distinct()
和ToList()
扩展方法。最后,您会注意到,单独进行此更改,在尝试更改循环中的字典时会遇到新的异常。您无法在
foreach
中更新集合。执行您想要的操作的最简单方法是完全省略显式循环。考虑这使用了另一个 Linq 扩展方法
ToDictionary()
。注意:这会在内存中创建一个新字典并更新d
以引用它。如果您需要保留d
引用的原始字典,那么您需要采用另一种方式。这里一个简单的选择是构建一个字典来隐藏d
,然后用它更新d
。这两个循环是安全的,但您会发现需要循环两次以避免在枚举原始集合的同时更新原始集合的问题,同时还将原始集合保留在内存中。
First, about the usage of the method.
Drop the cast, invoke
ToList()
on the result of the method. The result of the method isIEnumerable<string>
, this is not aList<string>
. The fact the source is originally aList<string>
is irrelevant, you don't return the list, youyield return
a sequence.Second, your
deDuplicateCollection
method is redundant,Distinct()
already exists in the library and performs the same function.Just be sure you have a
using System.Linq;
in the directives so you can use theseDistinct()
andToList()
extension methods.Finally, you'll notice making this change alone, you run into a new exception when trying to change the dictionary in the loop. You cannot update the collection in a
foreach
. The simplest way to do what you want is to omit the explicit loop entirely. ConsiderThis uses another Linq extension method,
ToDictionary()
. Note: this creates a new dictionary in memory and updatesd
to reference it. If you need to preserve the original dictionary as referenced byd
, then you would need to approach this another way. A simple option here is to build a dictionary to shadowd
, and then updated
with it.These two loops are safe, but you see you need to loop twice to avoid the problem of updating the original collection while enumerating over it while also preserving the original collection in memory.
已经有一个
Distinct
扩展方法来删除重复项!There is already a
Distinct
extension method to remove duplicates!