简体   繁体   English

迭代字典键/值对,其中值是数组

[英]Iterate through dictionary key/value pairs where the value is an array

I am trying to display the elements of a key/value pair from a dictionary where the value is an array. 我试图从值为数组的字典中显示键/值对的元素。 It is a dictionary of usernames where the key is a string representing the name of the user and the values are the users attributes. 它是用户名字典,其中键是表示用户名称的字符串,值是用户属性。 I want each key/value pair to be listed together, so the output would be like this: 我希望每个键/值对一起列出,因此输出将如下所示:

User1 (Key) 用户1(密钥)
User1_email User1_email
user1_fullName user1_fullName
User1_displayName User1_displayName

User2 (Key) 用户2(密钥)
User2_email User2_email
user2_fullName user2_fullName
User2_displayName User2_displayName
etc... 等等...

However, the result I am getting is that it is displaying the correct key but only the last elements of the value are being displayed. 但是,我得到的结果是它显示正确的键,但只显示值的最后一个元素。 For example, here is the output I am getting: 例如,这是我得到的输出:

User1 (Key) 用户1(密钥)
User2_email User2_email
user2_fullName user2_fullName
User2_displayName User2_displayName

User2 (Key) 用户2(密钥)
User2_email User2_email
user2_fullName user2_fullName
User2_displayName User2_displayName
etc... 等等...

Here is my foreach loop: 这是我的foreach循环:

foreach (KeyValuePair<string, string[]> entry in userNames)
{
    txtOutput.Text += entry.Key;
    foreach (string value in entry.Value)
    {
        txtOutput.Text += value + Environment.NewLine;
    }
}

The nested foreach loop seems to be iterating over ALL of the values regardless of the key, but I only want it to iterate over the values associated with the CURRENT key in the parent foreach loop. 嵌套的foreach循环似乎在迭代所有值而不管键,但我只希望它迭代与父foreach循环中的CURRENT键相关联的值。 Hope my question makes sense, any help is appreciated. 希望我的问题有道理,任何帮助都表示赞赏。

Thank you, 谢谢,

You gave the following code in a comment for populating your dictionary: 您在注释中提供了以下代码以填充字典:

string[] tempArray = new string[5];

for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < 6; j++)
    {
        if (j != 0) tempArray[j-1] = userInfo[i, j];
    }
    userNames.Add(userInfo[i, 0], tempArray);
}

It looks like you were hoping to reduce memory usage by using a single array outside the for loop. 看起来你希望通过在for循环之外使用单个数组来减少内存使用量。 However, this has an unexpected side-effect on your dictionary! 但是,这会对您的字典产生意想不到的副作用!

Dictionaries will store arrays by reference. 字典将通过引用存储数组。 On the first iteration of the populating loop, you call usernames.Add(..., tempArray) which assigns tempArray instance to the first user key. 在填充循环的第一次迭代中,您调用usernames.Add(..., tempArray) ,它将tempArray实例分配给第一个用户键。 Then the second iteration re-uses the same array and assigns it to the next user key. 然后第二次迭代重新使用相同的数组并将其分配给下一个用户密钥。 Since you are not initializing a new array instance for each user, you are actually associating all of your keys with the same array. 由于您没有为每个用户初始化新的数组实例,因此实际上是将所有键与同一个数组相关联。 The final iteration of the loop will determine the state of this array. 循环的最后一次迭代将确定该数组的状态。

This should fix it: 这应该解决它:

for (int i = 0; i < rows; i++)
{
    string[] tempArray = new string[5];
    for (int j = 0; j < 6; j++)
    {
        if (j != 0) tempArray[j-1] = userInfo[i, j];
    }
    userNames.Add(userInfo[i, 0], tempArray);
}

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

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