[英]Retrieving values from Dictionary<string, List<string>>
I have dictionary with keys containing a list of strings. 我有带有包含字符串列表的键的字典。
Dictionary<string, List<string>> keyValueListDict=new Dictionary <string,List<string>>();
List<string>valueList = new List<string>();
My keyValueListDict
is filled with unique keys and valueLists. 我的keyValueListDict
充满了唯一的键和valueLists。 For each dictionary entry, I need to output the key followed by the contents of the list (value). 对于每个字典条目,我需要输出键,然后输出列表的内容(值)。
Update: I think the problem is coming from entering the values So i am retrieving an object from my db 更新:我认为问题来自输入值,所以我正在从数据库中检索对象
foreach(object)
{
key=object.key;
foreach(string value in object.othercomponent)
{
list.add(value);
}
keyValueListDict.add(key,list);
list.clear();
}
The count for the values in the list resets to 0 everytime I clear the list so I can fill it for the next object, but I can't figure out how to avoid this 每次清除列表时,列表中值的计数都会重置为0,以便可以为下一个对象填充它,但我不知道如何避免这种情况
尝试这个:
var valueList = keyValueListDict.SelectMany(kvp => kvp.Value).ToList();
This is the basic approach to iterate over a dictionary: 这是遍历字典的基本方法:
var keyValueListDict = new Dictionary<string, List<string>>() {
{"fookey", {"foo", "bar", "baz"}},
{"barkey", {"bar", "baz", "foo"}},
{"bazkey", {"baz", "foo", "bar"}}
};
foreach(var entry in keyValueListDict) {
string key = entry.Key;
List<string> values = entry.Value;
//Console.WriteLine(key + " => " + values);
//Console.WriteLine(key + " => [" + string.Join(", ", values) + "]");
}
Try something like this to create one output string of the dictionary's entry key followed by the combined values. 尝试执行类似的操作,以创建字典的输入键的一个输出字符串,然后是组合值。
public string GetOutputString(KeyValuePair<string, List<string>> kvp)
{
string separator = ", "; //you can change this if needed (maybe a newline?)
string outputString = kvp.Key + " " + String.Join(separator, kvp.Value);
return outputString;
}
If you only had the key (and not the entire KeyValuePair
), you could create a copy from your dictionary with something like (see also this question about getting a KeyValuePair
directly from a dictionary) 如果只有键(而不是整个KeyValuePair
),则可以从字典中创建类似以下内容的副本(另请参见有关直接从字典中获取KeyValuePair
问题 )
var kvp = new KeyValuePair<string, List<string>>(key, keyValueListDict[key]);
If you needed to output the entire dictionary, you could do something like this: 如果需要输出整个字典,则可以执行以下操作:
foreach (var kvp in keyValueListDict)
{
string output = GetOutputString(kvp);
//print output, write to file etc.
}
Try this: 尝试这个:
foreach (var key in dictionary.Keys)
{
var tmp = string.Empty;
foreach (var value in dictionary[key])
{
tmp +=value """";
System.Console.WriteLine(tmp );
}
}
I has this problem today, and I didn't like being forced to change my data structure, I really wanted to maintain the List<string>
. 今天我遇到了这个问题,我不喜欢被迫更改数据结构,我真的很想维护List<string>
。 This worked for me. 这对我有用。
var reportTabGridItems = expectedReportTabsWithReportItems.Where(x => x.Key == reportTabName).Select(x => x.Value).FirstOrDefault();
I just wanted one row, therefore I tacked on the FirstOrDefault()
to the end since the last select will bring back a list of list<string>
and I just want one (or null if not found). 我只想要一行,因此我将FirstOrDefault()
最后,因为最后一次选择将带回list<string>
的列表,而我只想要一个(如果找不到则为null)。
Hope this helps you. 希望这对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.