Solved

dictionary of hashsets

Posted on 2010-08-30
6
799 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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 This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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