Link to home
Start Free TrialLog in
Avatar of dba123
dba123

asked on

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


Avatar of surajguptha
surajguptha
Flag of United States of America image

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
Avatar of dstanley9
dstanley9

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]);
Avatar of dba123

ASKER

Thank you.

isn't there a way to do both key and value in one snap?  one method call?
ASKER CERTIFIED SOLUTION
Avatar of dstanley9
dstanley9

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dba123

ASKER

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>>'
Avatar of dba123

ASKER

what do you mean by dictionary within dictionary.  It's one dictionary.
I noticed you closed the question - Did you get it working?  

What data type is dicRights?
Avatar of dba123

ASKER

shoot I'm sorry it's a Dictionary (generic)
Avatar of dba123

ASKER

the dictionary expects a string key and generic list as the value type of arguments