Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have a dictionary where both key and value are of type string.
Values are starting with capital letter like "Test".
I have to check whether this dictionary contains user input strings. User inputed strings can be of any case like mix of lower/upper case.

How do I convert dictionary values into lower case and compare against lower case converted user input values?

Like

Dictionary <string,string> str = new Dictionary<string,string>();
str.Add("1","Test");

now user input string can be "test". i will do it ToLower() on user input string. How do I use dictionary's contains method? how do I convert dictionary values into lower case and compare it? Remember I dont have key now..

Please suggest
Posted

Lambda Expressions ...
if(str.Select(x=>x.Value).Where(x=>x.ToLower()==test.ToLower()).Count()>0)
{/*Code*/}


if you are familiar with it this should be a quick code for manipulating collection items
 
Share this answer
 
Comments
Nish Nishant 4-Aug-10 16:27pm    
Reason for my vote of 5
Proposed as answer.
AspDotNetDev 4-Aug-10 16:37pm    
Iterating the entire dictionary for each lookup seems inefficient.
Either add them to your dictionary as all upper or all lower case, or convert the user input to the correct type and check. Unlike String.Equals, Dictionary.ContainsValue has no override to specify case sensitivity. You could override the comparer, but that is more work that I would do for something this simple.
 
Share this answer
 
Create a second dictionary:
C#
Dictionary<String, List<String>> reversed;

The key in this dictionary will be the lowercase version of the value from the original dictionary. The list of strings in the value is because you can have mulitple keys in the original dictionary with different keys but the same value. Whenever you insert an entry into the original dictionary, you do so again for the reversed dictionary.

When you get the value from this dictionary, you can go over the list of strings to lookup values in the original dictionary and check if they are uppercase or lowercase.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900