Solved

dictionary of hashsets

Posted on 2010-08-30
6
813 Views
Last Modified: 2012-06-22
I have a dictionary of hashsets. I need a method search through a dictionary of hashsets to find a match.
Something like below code
public bool IsMatch(string lookup, Dictionary d)
{
 bool match;
 for each (hashet x in Dictionary.Keys)
 {
if (lookup match(x.key))
{return match;}
 }
}
But I'm not exactly sure how of the syntax of accessing the dictionary's keys & values.
My dictionary initialization in the main method looks like this:
Dictionary<string, HashSet<string>> l = new Dictionary<string, HashSet<string>>();
and it's populated already when I want to call this method
IsMatch("yada")

public bool IsMatch(string lookup, Dictionary d)
{
 bool match;
 for each (hashet x in Dictionary.Keys)
 {
if (lookup match(x.key))
{return match;}
 }
}

Open in new window

0
Comment
Question by:mielcita
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33563116
Is "yada" a key in the dictionary or a value in one of the Hashsets?
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33566274
try this

if(l.ContainsKey(_key))
return l[_key);

return null;

If this is not what you're expecting, brief what the method is suppose to look for and return what.
0
 

Author Comment

by:mielcita
ID: 33570722
Novice,
Thanks, I tried that, and it does not return any values.

Kaufmed,
"yada" is a string I'm trying to match.
For testing purposes I'm using a string that is included in one of the hashsets.
But
l.ContainsKey("yada")
or
l.ContainsKey(_key)  //where _key is a string in one hashset

still returns null.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 33571552
If "yada" is a value in one of the HashSets, then you would search like this:
Dictionary<string, HashSet<string>> l = new Dictionary<string, HashSet<string>>();
bool found;

foreach (var item in l)
{
    if (item.Value.Contains("yada"))
    {
        found = true;
        break;
    }
}

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 33571563
Note:

I used "var" as a shortcut because it's easier to write than what I'm about to show you. You could also have done:
Dictionary<string, HashSet<string>> l = new Dictionary<string, HashSet<string>>();
bool found;

foreach (KeyValuePair<string, HashSet<string>> item in l)
{
    if (item.Value.Contains("yada"))
    {
        found = true;
        break;
    }
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33571749
You could also have done this with LINQ or lambdas:
// LINQ
var result = from item in l
             where item.Value.Contains("yadda")
             select item;

bool found = result.Count() > 0;

// Lambda

var result = l.Values.FirstOrDefault(item => item.Contains("yadda"));

bool found = (result != null);

Open in new window

0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question