Look up key value pair in Generic Dictionary

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


LVL 1
dba123Asked:
Who is Participating?
 
dstanley9Commented:
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
 
surajgupthaCommented:
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
 
dstanley9Commented:
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
dba123Author Commented:
Thank you.

isn't there a way to do both key and value in one snap?  one method call?
0
 
dba123Author Commented:
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
 
dba123Author Commented:
what do you mean by dictionary within dictionary.  It's one dictionary.
0
 
dstanley9Commented:
I noticed you closed the question - Did you get it working?  

What data type is dicRights?
0
 
dba123Author Commented:
shoot I'm sorry it's a Dictionary (generic)
0
 
dba123Author Commented:
the dictionary expects a string key and generic list as the value type of arguments
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.