Solved

dictionary of hashsets

Posted on 2010-08-30
6
789 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 74

Expert Comment

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

Expert Comment

by:Gururaj Badam
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
Comment Utility
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 74

Expert Comment

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now