This question might appear to be a duplicate and/or too boring, but I want to do this using this specific method.
When a user enters a string into a textbox I want to get this string key. So to do this I've created a dictionary which has n (n <= 15000) unique values. I want to get the key from this dictionary by value. The method below works well:
Dictionary<int, string> artikullar = new Dictionary<int, string>();
int key = (from elem in artikullar where elem.Value == txt_artikul.Text select elem).First().Key;
Before that I tried to use the First() method to get the key:
int key = artikullar.AsParallel().First(new Func<KeyValuePair<int, string>, bool>(val => val == txt_artikul.Text)).Key;
But it throws this error:
Operator '==' cannot be applied to operands of type 'KeyValuePair' and 'string'
I haven't used this method before.
Any helpful comment or answer would be appreciated.
Dictionary<int, string>, shoulden't it be aDictionary<string, int>if you are doing the lookup by the string? Also, if you have the same value for multiple keys your function may return a differentkeyeach time you run the function, is that acceptable?