简体   繁体   中英

How do I iterate over a hashset of dictionaries

Apologies in advance; I am on day 3 of trying to learn C#.

I am instructed to build a hashset of dictionaries; no problem. I see that that has been built. I now need to iterate over the hashset and copy the entries to a new list if the dictionary's key != a particular string. Could someone kindly explain the proper syntax for achieving this seemingly simple task?

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

Goal - To get unstuck, hope to learn C#, and decide if I want to keep going down this rabbit hole. Thanks to all.

Here is an example of how to iterate through the hashset and then the dictionary:

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

        Dictionary<string, string> newDict = new Dictionary<string, string>();
        newDict.Add("M", "T");
        all.Add(newDict);

        foreach(Dictionary<string,string> dict in all)
        {
            foreach(KeyValuePair<string,string> pair in dict)
            {
                string key = pair.Key;
                string value = pair.Value;
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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