2

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.

2
  • 1
    Why are you holding a Dictionary<int, string>, shoulden't it be a Dictionary<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 different key each time you run the function, is that acceptable? Commented Feb 18, 2016 at 17:03
  • @ScottChamberlain thanks to quick response. Well, the values should be stored as type of string and keys as type of int. So, this syntax is wrong ? I have wrote that, dictionary has n (n <= 15000) unique values. Commented Feb 18, 2016 at 17:08

2 Answers 2

7

Ehsan Sajjad is correct. But I'd like to add another point:

You said that the strings in your Dictionary are unique. I don't know when you created the Dictionary and if the data is rather static or dynamically changing all the time.

If it's rather static and the strings are unique, you may consider to create a reversed Dictionary like this:

Dictionary<string, int> reversedDict = artikullar.ToDictionary(
    kvp => kvp.Value,
    kvp => kvp.Key);

and then use this for your lookup:

int key = reversedDict[txt_artikul.Text];

This may be faster than querying the original dictionary.

Sign up to request clarification or add additional context in comments.

Comments

3

You have to change :

val => val == txt_artikul.Text

to:

val = > val.Value == txt_artikul.Text

you have instance of KeyValuePair, you have to specify Value in lambda expression to be compared.

8 Comments

While this does solve the specific problem the OP asked about his design choice is a very poor one. He will not get any of the O(1) benefits of a dictionary doing this and his lookup could be very slow.
you may be right, but question is about the problem he is facing with lambda expression
@MirjalalTalishinski use two dictionaries, one for lookups by int, one for lookups by string. Keep them in sync whenever you add or remove a value, whatver you do to one, do to the other.
See René Vogt's answer for a example.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.