首页 > 解决方案 > Parallel.ForEach 具有可枚举的 KeyValuePairs?

问题描述

我是否使用了错误的 Parallel ForEach 重载方法?当我使用普通的 ForEach 循环时,我能够获取当前项目,它是预期的正确类型 (KeyValuePair)。

但是当我使用 Parallel 版本时,似乎即使我将鼠标悬停在循环中的当前对象上并且它似乎是正确的类型,我仍然没有得到 Value 和 Key 属性。

提前致谢!

static void TestParallelForEachKeyValuePair(IEnumerable<KeyValuePair<Animal, string>> kvps)
    {
        foreach (var kvp in kvps)
        {
            var test = kvp.Key;
        }

        Parallel.ForEach(kvps, (kvp) =>
        {
            kvp.
        });
    }

为每个

并行 ForEach

将当前项目悬停在 Parallel ForEach 中

标签: c#.netcollectionstask-parallel-library

解决方案


此测试方法有效,但 IntelliSense 未显示“键”和“值”(Windows 窗体)。所以它的智能感知问题。

    static void TestParallelForEachKeyValuePair()
    {
        List<KeyValuePair<int, string>> test = new List<KeyValuePair<int, string>>();
        test.Add(new KeyValuePair<int, string>(1, "test1"));
        test.Add(new KeyValuePair<int, string>(2, "test2"));
        test.Add(new KeyValuePair<int, string>(3, "test3"));


         Parallel.ForEach(test, (x) =>
         {
             MessageBox.Show(x.Key + "  " + x.Value);
         });
    }

推荐阅读