?
Solved

Look up key value pair in Generic Dictionary

Posted on 2007-10-11
9
Medium Priority
?
2,014 Views
Last Modified: 2008-01-09
I"m trying to check weather a value exists in the Dictionary.  The dictionary is defined for example with a string key and then the value is a string of comma delimited values like this

So I'm trying to check whether a single string value exists in the Dictionary for a specific key and in the value's list

Here I just populate the Dictionary with a Generic string for the value of the key value pair

        private void setupDictionary()
        {
            List<string> list1 = null;
            List<string> list2 = null;

            list1.Add("Print");
            list1.Add("Read");
            list1.Add("Insert");
            list1.Add("Update");
            list1.Add("Delete");

            list2.Add("Read");
            list2.Add("Print");

            dicRights.Add("John", list1);
            }

Now I'm trying to check whether a another list value is in the dictionary.  So lets say there is only one record in the generic list rightsRequested such as "Delete".  I need to check whether this user has delete permissions by seeing if the dictionary contains that key (the user's name) and if "Delete" is in the lkist of value for that user.

bool result = dicRights.Contains(principal.Identity.Name, rightsRequested[0]);

So the error I get on the line above is with the rightsRequested.  Doesn't look Iike I can use the method Contains on the dictionary to do what I want...so what method could I use or am I going about this lookup wrong in terms of looking up key values like this in a Dictionary as I have set this up?

Error: No overload for method 'Contains' takes '2' arguments


0
Comment
Question by:dba123
  • 5
  • 3
9 Comments
 
LVL 21

Expert Comment

by:surajguptha
ID: 20058382
bool result = dicRights.Contains(principal.Identity.Name, rightsRequested[0]);

First find the dictionary item called John
Then get a List<string> which contains the roles and then check to see if the role is present for the user
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 20058908
Is dicRights a Dictionary<string,List<string>>?  If so, use

bool result = false;
if(dicRights.Contains(principal.Identity.Name)
  result = dicRights[principal.Identity.Name].Contains(rightsRequested[0]);
0
 
LVL 1

Author Comment

by:dba123
ID: 20059261
Thank you.

isn't there a way to do both key and value in one snap?  one method call?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Accepted Solution

by:
dstanley9 earned 2000 total points
ID: 20059310
Not the way you're storing the data, because you got a dictionary within a dictionary.  You could do:

bool result = dicRights[principal.Identity.Name].Contains(rightsRequested[0]);

but if the name isn't in the parent dictionary then you'll get a Null Reference Exception.

Basically you need to ask: a) "Is the user in the list", and b) "Does the user have the requested rights".  If a) is given then you can go with the one-line method
0
 
LVL 1

Author Comment

by:dba123
ID: 20059324
also tried that.  I get this error and not quite sure what it's expecting then.  I thought Contains was expecting a string

talking about line if(dicRights.Contains(principal.Identity.Name))

Error: The best overloaded method match for 'System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string,System.Collections.Generic.List<string>>>.Contains(System.Collections.Generic.KeyValuePair<string,System.Collections.Generic.List<string>>)' has some invalid arguments      

Error: Argument '1': cannot convert from 'string' to 'System.Collections.Generic.KeyValuePair<string,System.Collections.Generic.List<string>>'
0
 
LVL 1

Author Comment

by:dba123
ID: 20059362
what do you mean by dictionary within dictionary.  It's one dictionary.
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 20060770
I noticed you closed the question - Did you get it working?  

What data type is dicRights?
0
 
LVL 1

Author Comment

by:dba123
ID: 20062221
shoot I'm sorry it's a Dictionary (generic)
0
 
LVL 1

Author Comment

by:dba123
ID: 20062222
the dictionary expects a string key and generic list as the value type of arguments
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

807 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