Parallel.ForEach with Enumerable of KeyValuePairs?

问题描述 投票:1回答:1

我是否使用了并行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

Hovering the current item inside Parallel ForEach

c# .net collections task-parallel-library
1个回答
0
投票

此测试方法有效,但是IntelliSense不显示“键”和“值”(Windows窗体)。因此是IntelliSense问题。

    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);
         });
    }
© www.soinside.com 2019 - 2024. All rights reserved.