CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2014
    Posts
    2

    iterating through lists within a dictionary

    Hi

    c# newbie here just to set the expectations!

    Ive set up a dicitionary of Lists and Im trying tho loop through but cannot seem to get the Dicitonary Key :-(

    //set up e dicitonary
    Dictionary<string, List<string>> hdrAttrAtDict = new Dictionary<string, List<string>>();

    //found this to iterte through the contents of the list within the dictionary but not sure how to access the dictionary key itself?
    foreach (List<string> list in hdrAttrAtDict.Values)
    {

    //list.
    foreach (string entry in list)
    {
    //iterating the list of strings for the current dictionary entry.
    logger.Debug("hdrAttrAtDict Dictionary: Key[" + "]/--> Value[" + entry + "]...");
    }
    }


    Any help appreiate.. or perhaps there is a better way?

    Kind regards
    Satnam

  2. #2
    Join Date
    Dec 2014
    Posts
    2

    Re: iterating through lists within a dictionary

    ha..

    having thought about it I may have resolved it with the below:
    foreach (KeyValuePair<string, List<string>> entry in hdrAttrAtDict)
    {
    logger.Debug("Dictionary hdrProdTypeColCountsDict-Key[" + entry.Key + "]--> Value[" + "]..");
    foreach (string listEntry in entry.Value)
    {
    logger.Debug("Dictionary hdrProdTypeColCountsDict-Key[" + entry.Key + "]--> Value[" + listEntry + "]..");

    }
    }

    Would be interested to know if thre was a more concise way of doing it though?

    cheers
    S

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: iterating through lists within a dictionary

    You can use LINQ.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured