我如何遍历字典的哈希集

提前致歉;我在尝试学习 C# 的第 3 天。


我被指示建立一个字典哈希集;没问题。我看到那已经建成了。如果字典的键 != 特定字符串,我现在需要遍历哈希集并将条目复制到新列表。有人可以解释一下实现这个看似简单的任务的正确语法吗?


var goodSongs = new List<Dictionary<string,string>>();


var allSongs = new HashSet<Dictionary<string, string>>();


Dictionary<string, string> meatloaf = new Dictionary<string, string>();

meatloaf.Add("Meatloaf", "Paradise By The Dashboard Light");


Dictionary<string, string> aerosmith = new Dictionary<string,string>();

aerosmith.Add("Aerosmith", "Cryin'");


Dictionary<string, string> nickelback = new Dictionary<string, string>();

nickelback.Add("Nickelback", "Rockstar");


allSongs.Add(nickelback);

allSongs.Add(aerosmith);

allSongs.Add(meatloaf);


//foreach loop to iterate dictionaries goes here

目标 - 摆脱困境,希望学习 C#,并决定我是否要继续探索这个兔子洞。谢谢大家。


倚天杖
浏览 101回答 1
1回答

天涯尽头无女友

这是一个如何遍历哈希集然后遍历字典的示例:&nbsp; &nbsp; &nbsp; &nbsp; var all = new HashSet<Dictionary<string, string>>();&nbsp; &nbsp; &nbsp; &nbsp; Dictionary<string, string> newDict = new Dictionary<string, string>();&nbsp; &nbsp; &nbsp; &nbsp; newDict.Add("M", "T");&nbsp; &nbsp; &nbsp; &nbsp; all.Add(newDict);&nbsp; &nbsp; &nbsp; &nbsp; foreach(Dictionary<string,string> dict in all)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach(KeyValuePair<string,string> pair in dict)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string key = pair.Key;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string value = pair.Value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP