Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

dictionary of hashsets

Posted on 2010-08-30
6
Medium Priority
?
824 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1000 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 1000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

971 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