Solved

dictionary of hashsets

Posted on 2010-08-30
6
792 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Data Saving 5 56
Loop not working 29 47
Remove greater than sign 3 42
C# Single Form 8 26
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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

930 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

18 Experts available now in Live!

Get 1:1 Help Now