Results 1 to 6 of 6

Thread: [RESOLVED] Need help with searching dictionary values in another dictionary

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Resolved [RESOLVED] Need help with searching dictionary values in another dictionary

    Hello, I have two dictionaries and want to search one against another. I tried:

    Code:
    foreach (KeyValuePair<int, string> entry in requestedNames)
    {
    	// do something with entry.Value or entry.Key
    	if (allNames.ContainsValue(entry.Value))
    	{
    		Console.WriteLine(entry.Value);
    	}
    }
    but this doesn't work. It doesn't output anything.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help with searching dictionary values in another dictionary

    Please don't expect us to work what you're trying to achieve from code that doesn't actually do it. Explain it. This:
    I have two dictionaries and want to search one against another.
    is simply too vague. Provide a FULL and CLEAR explanation of what you're trying to achieve.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Need help with searching dictionary values in another dictionary

    Thank you for your reply. Well I'm trying to check if the programs that my program installed are in the list of installed programs.

    I have a dictionary (requestedNames) which I want to check if its values matches another dictionary named allNames. Here's how I declare them.

    requestedNames:

    C# Code:
    1. Dictionary<int, string> requestedNames = new Dictionary<int, string>();
    2. int num = 0;
    3. try
    4. {
    5.     // Create an instance of StreamReader to read from a file.
    6.     // The using statement also closes the StreamReader.
    7.     using (StreamReader sr = new StreamReader(@"program_names.txt"))
    8.     {
    9.         string line;
    10.         // Read and display lines from the file until the end of
    11.         // the file is reached.
    12.         while ((line = sr.ReadLine()) != null)
    13.         {
    14.             requestedNames.Add(num, line);
    15.             num++;
    16.         }
    17.     }
    18. }
    19. catch (Exception ex)
    20. {
    21.     // Let the user know what went wrong.
    22.     Console.WriteLine("The file could not be read:");
    23.     Console.WriteLine(ex.Message);
    24. }

    allNames:

    C# Code:
    1. num = 0;
    2.  
    3. Dictionary<int, string> allNames = new Dictionary<int, string>();
    4. string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
    5. using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
    6. {
    7.     foreach (string subkey_name in key.GetSubKeyNames())
    8.     {
    9.         using (RegistryKey subkey = key.OpenSubKey(subkey_name))
    10.         {
    11.             if (subkey.GetValue("DisplayName") != null)
    12.             {
    13.                 allNames.Add(num, subkey.GetValue("DisplayName").ToString());
    14.                 num++;
    15.             }
    16.         }
    17.     }
    18. }

    I mean I want to check if values in requestedNames are in allNames.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help with searching dictionary values in another dictionary

    If you want a list of values from one Dictionary that are also values in another, this should do the job:
    vb.net Code:
    1. var matches = dictionary1.Values.Intersect(dictionary2.Values).ToArray();
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Need help with searching dictionary values in another dictionary

    Thanks for the hint. However I'm still having problem with my code. This outputs 1 where it should output 2:

    VB.NET Code:
    1. var matches = allNames.Values.Intersect(requestedNames.Values).ToArray();
    2. Console.WriteLine(matches.Length);

    I have programs installed 7-Zip 16.04 (x64 edition) and AIMP. And these are at line 1 and 2 in program_names.txt. So matches.Length should output 2, right? How can I fix this?

    EDIT: When I debug the code, I can see num increases, two lines are added.
    Last edited by nikel; Aug 10th, 2017 at 09:37 AM. Reason: I added debug info

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Need help with searching dictionary values in another dictionary

    Anyway I manually searched in allNames but couldn't find it. Thanks for the help.

Tags for this Thread

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