重复的 IEnumerable、列表和强制转换

发布于 2024-12-10 07:20:22 字数 798 浏览 0 评论 0原文

阅读此关于重复删除的非常有趣的线程后,我结束了有了这个=>

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

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

发布评论

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

评论(2

可可 2024-12-17 07:20:22

首先,关于方法的使用。

放弃强制转换,对方法的结果调用 ToList()。该方法的结果是 IEnumerable,这不是 List。源最初是一个List这一事实是无关紧要的,您不返回列表,您产生返回一个序列。

d[kvp.Key] = deDuplicateCollection(kvp.Value).ToList();

其次,您的 deDuplicateCollection 方法是多余的,Distinct() 已经存在于库中并执行相同的功能。

d[kvp.Key] = kvp.Value.Distinct().ToList();

只需确保指令中有 using System.Linq; 即可使用这些 Distinct()ToList() 扩展方法。

最后,您会注意到,单独进行此更改,在尝试更改循环中的字典时会遇到新的异常。您无法在 foreach 中更新集合。执行您想要的操作的最简单方法是完全省略显式循环。考虑

d = d.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Distinct().ToList());

这使用了另一个 Linq 扩展方法 ToDictionary()。注意:这会在内存中创建一个新字典并更新d以引用它。如果您需要保留 d 引用的原始字典,那么您需要采用另一种方式。这里一个简单的选择是构建一个字典来隐藏 d,然后用它更新 d

var shadow = new Dictionary<string, string>();
foreach (var kvp in d)
{ 
    shadow[kvp.Key] = kvp.Value.Distinct().ToList();
}

foreach (var kvp in shadow)
{
    d[kvp.Key] = kvp.Value;
}

这两个循环是安全的,但您会发现需要循环两次以避免在枚举原始集合的同时更新原始集合的问题,同时还将原始集合保留在内存中。

First, about the usage of the method.

Drop the cast, invoke ToList() on the result of the method. The result of the method is IEnumerable<string>, this is not a List<string>. The fact the source is originally a List<string> is irrelevant, you don't return the list, you yield return a sequence.

d[kvp.Key] = deDuplicateCollection(kvp.Value).ToList();

Second, your deDuplicateCollection method is redundant, Distinct() already exists in the library and performs the same function.

d[kvp.Key] = kvp.Value.Distinct().ToList();

Just be sure you have a using System.Linq; in the directives so you can use these Distinct() and ToList() 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. Consider

d = d.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Distinct().ToList());

This uses another Linq extension method, ToDictionary(). Note: this creates a new dictionary in memory and updates d to reference it. If you need to preserve the original dictionary as referenced by d, then you would need to approach this another way. A simple option here is to build a dictionary to shadow d, and then update d with it.

var shadow = new Dictionary<string, string>();
foreach (var kvp in d)
{ 
    shadow[kvp.Key] = kvp.Value.Distinct().ToList();
}

foreach (var kvp in shadow)
{
    d[kvp.Key] = kvp.Value;
}

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.

憧憬巴黎街头的黎明 2024-12-17 07:20:22
d[kvp.Key] = kvp.Value.Distinct().ToList();

已经有一个 Distinct 扩展方法来删除重复项!

d[kvp.Key] = kvp.Value.Distinct().ToList();

There is already a Distinct extension method to remove duplicates!

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