13,767,441 members
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
v2

## 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;
enumerator.MoveNext();
}```
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

5ed!

## 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>();

//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

//display
foreach (var key in respectiveKeys)
{
Console.WriteLine(key);
}```
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).
Cheers,
Maciej
Thomas D [ProgramFOX] 3-Jan-18 12:21pm

Yep, I voted 4.
Maciej Los 3-Jan-18 14:27pm

Thanks again :D

Top Experts
Last 24hrsThis month
 OriginalGriff 466 CPallini 230 CHill60 195 Aydin Homay 114 Richard MacCutchan 95
 OriginalGriff 2,842 Richard MacCutchan 1,715 CPallini 904 MadMyche 785 CHill60 495