|
-
Aug 9th, 2017, 06:05 PM
#1
Thread Starter
Addicted Member
[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.
-
Aug 9th, 2017, 08:30 PM
#2
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.
-
Aug 10th, 2017, 06:41 AM
#3
Thread Starter
Addicted Member
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:
Dictionary<int, string> requestedNames = new Dictionary<int, string>();
int num = 0;
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(@"program_names.txt"))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
requestedNames.Add(num, line);
num++;
}
}
}
catch (Exception ex)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(ex.Message);
}
allNames:
C# Code:
num = 0;
Dictionary<int, string> allNames = new Dictionary<int, string>();
string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
{
foreach (string subkey_name in key.GetSubKeyNames())
{
using (RegistryKey subkey = key.OpenSubKey(subkey_name))
{
if (subkey.GetValue("DisplayName") != null)
{
allNames.Add(num, subkey.GetValue("DisplayName").ToString());
num++;
}
}
}
}
I mean I want to check if values in requestedNames are in allNames.
-
Aug 10th, 2017, 06:49 AM
#4
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:
var matches = dictionary1.Values.Intersect(dictionary2.Values).ToArray();
-
Aug 10th, 2017, 09:32 AM
#5
Thread Starter
Addicted Member
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:
var matches = allNames.Values.Intersect(requestedNames.Values).ToArray(); 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
-
Aug 10th, 2017, 09:45 AM
#6
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|