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(princip al.Identit y.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
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(princip
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
Is dicRights a Dictionary<string,List<str ing>>? If so, use
bool result = false;
if(dicRights.Contains(prin cipal.Iden tity.Name)
result = dicRights[principal.Identi ty.Name].C ontains(ri ghtsReques ted[0]);
bool result = false;
if(dicRights.Contains(prin
result = dicRights[principal.Identi
ASKER
Thank you.
isn't there a way to do both key and value in one snap? one method call?
isn't there a way to do both key and value in one snap? one method call?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(prin cipal.Iden tity.Name) )
Error: The best overloaded method match for 'System.Collections.Generi c.ICollect ion<System .Collectio ns.Generic .KeyValueP air<string ,System.Co llections. Generic.Li st<string> >>.Contain s(System.C ollections .Generic.K eyValuePai r<string,S ystem.Coll ections.Ge neric.List <string>>) ' has some invalid arguments
Error: Argument '1': cannot convert from 'string' to 'System.Collections.Generi c.KeyValue Pair<strin g,System.C ollections .Generic.L ist<string >>'
talking about line if(dicRights.Contains(prin
Error: The best overloaded method match for 'System.Collections.Generi
Error: Argument '1': cannot convert from 'string' to 'System.Collections.Generi
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?
What data type is dicRights?
ASKER
shoot I'm sorry it's a Dictionary (generic)
ASKER
the dictionary expects a string key and generic list as the value type of arguments
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