Click here to Skip to main content
14,272,287 members
Rate this:
Please Sign up or sign in to vote.
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
Rate this:
Please Sign up or sign in to vote.

Solution 2

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
   
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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Create a second dictionary:
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.
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month