Click here to Skip to main content
13,767,441 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have an dictionary with string type contains :
keys values
1 ---- 3
2 ---- 5
3 ---- 4
4 ---- 3
5 ---- 10
6 ---- 3
7 ---- 5
8 ---- 9
9 ---- 3
10 ---- 3

the program choose values according some criteria say the program choose the values (3,3,3) and I need to match these values with its keys the results should be:
(1,4,6) respectively. how I can do that!

What I have tried:

string myKey = dictinonary.FirstOrDefault(x => x.Value == values[i]).Key;
Posted 2-Jan-18 7:59am
Updated 2-Jan-18 21:59pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Here is a way to do it:
int[] values = new int[] { 3, 3, 3 };
IEnumerator<KeyValuePair<int, int>> enumerator = dictionary.GetEnumerator();
int[] keys = new int[values.Length];
for (int i = 0; i < keys.Length; i++)
    while (enumerator.Current.Value != values[i])
        if (!enumerator.MoveNext())
            // not all values could be found
    keys[i] = enumerator.Current.Key;
Here's how it works:
  1. Declare the values you want to find.
  2. Get the enumerator[^] of the dictionary.
  3. Declare an array in which the keys will get stored.
  4. Loop over the values you want to find:
    1. If the value of the current KeyValuePair of the enumerator is not the current value we're looking for, we move the enumerator and repeat this check. If MoveNext returns false, there are no more elements so not all values could be found.
    2. Once the current enumerator value is what we are looking for, we store the key of the current KeyValuePair.
    3. Then we move the enumerator again, to avoid that we get the same key again if the next element in 'values' is the same.

  5. Now you have the keys array with all keys.

The enumerator makes it easy to go over the dictionary without having to keep track of an extra index.
Love Allah 2-Jan-18 16:47pm
thank u very much it worked
Thomas D [ProgramFOX] 2-Jan-18 16:47pm
You're welcome!
Maciej Los 2-Jan-18 17:26pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Answer #1 by ProgramFOX[^] is very good. In addition to it, i'd like to share another solution which uses Linq.

//create dictionary object and full fill it
Dictionary<int, int> dict = new Dictionary<int, int>();
dict.Add(1, 3);
dict.Add(2, 5);
dict.Add(3, 4);
dict.Add(4, 3);
dict.Add(5, 10);
dict.Add(6, 3);
dict.Add(7, 5);
dict.Add(8, 9);
dict.Add(9, 3);
dict.Add(10, 3);

//create a list of values to find in dictionary
List<int> valuesToFind = new List<int>{3, 3, 3};

//get respective keys
var respectiveKeys = dict
	.Where(kvp=> valuesToFind.Any(y=>y==kvp.Value)) //compare searched values with values in dictionary object
	.Select(kvp=> new
		k = kvp.Key,
		//v = kvp.Value
	}); //get only respective (corresponding) keys
	//.Take(3); //use it, if you want to get only 3 result

foreach (var key in respectiveKeys)
Thomas D [ProgramFOX] 3-Jan-18 7:12am
Unfortunately this does not guarantee the correct order of the keys: try {9, 4} as values to find and the keys will be {3, 8} rather than {8, 3}.
Maciej Los 3-Jan-18 7:26am
I do not see such as requirement...
Thomas D [ProgramFOX] 3-Jan-18 8:30am
I might have imagined that requirement, but the OP states in his example that the keys "should be (1,4,6) respectively" and the choice of the word "respectively" made me assume that the order must be respected in all cases.

Either way, it's definitely a neat solution if the order does not matter.
Maciej Los 3-Jan-18 8:37am
As to me "respectively" means "get corresponding values".
Thomas D [ProgramFOX] 3-Jan-18 12:02pm
Ahh, I see. The definition of respectively is "separately or individually and in the order already mentioned" which made me think the order was important; of course I cannot tell whether OP's intention was my or your interpretation. It's possible I'm just nitpicking here :)
Maciej Los 3-Jan-18 12:19pm
OK. English is not my native language. So, it is quite possible i misunderstood OP's intention. Thank you for your time (and hopefuly up-vote).
Thomas D [ProgramFOX] 3-Jan-18 12:21pm
Yep, I voted 4.
Maciej Los 3-Jan-18 14:27pm
Thanks again :D

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

Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.181116.1 | Last Updated 3 Jan 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100