Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more: , +
hi i have a dictionary as _ABC
the values in dictionary are
(America, 54)
(bin, 244)
(bolivia, 694)
(china, 54)
(Denmark, 54)
(india, 94)
(japan, 44)
(zimbambe, 54)
i am using this line of code to get its key based on value
C#
string emailAdd = _ABC.FirstOrDefault(x => x.Value.Contains("94")).Key;

but it is returning value as bolivia.. but i need the result as India. how can i get this.

It is searching from top of dictionary and checking for a wrong value instead of required and returning its key ..
Posted

First of all, if you want exact match then you must compare the whole string
C#
x => x.Value == 94
.

Then, a dictionary does not have any order so you might get any match and not the desired one. Thus, if you really need to get the smallest key that have a specified value, that algorithm could fails if there are duplicate values. It might even appears to works sometime but it is really an implementation details and it is not defined if the actual order depends on the key, the key hash value, the insertion order and some other internal details like bucket size and such.

Finally, it would generally be a bad design choice if you need to find items by there value. A dictionary is really intented to find item by key. In some case, it might make sense to use a secondary dictionary for reverse lookup. In that case, you will need a list for values as their might be multilpe keys that have the same value as it would be the case for 54 in you example.

If values come from a database, then you should make the appropriate query to get desired result.

Other solutions are mostly correct except that in your case, you want to search for a given value and not the key.
 
Share this answer
 
There are two possible solutions I can think of:
First, assuming that your Dictionary entries are all really integer values, then define the Dictionary with int values, not as string:
C#
Dictionary<string, int> _ABC = new Dictionary<string, int>();
Then store the values as integers and your reverse lookup will behave:
C#
_ABC.Add("America", 54);  // NOT ("America", "54")
// ... etc
string emailAdd = _ABC.FirstOrDefault(x => x.Value == 94).Key;

This is not efficient.
There is no order preserved! This is unpredictable if the values are not unique.
I think a better solution would be just to keep 2 Dictionary instances. The _ABC you have now (and it is still better to keep the Value as int if that is really the correct constraint on the values). And then a second one for the reverse mapping:
C#
Dictionary<int, string> _ABCrev = new Dictionary<int, string>();
_ABCrev.Add(54, "America");
string emailAdd = _ABCrev[54];

Again, this counts on the numeric "values" being unique.
 
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