Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 2-Jan-18 20:59pm
v2

Here is a way to do it:
C#
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.
 
Share this answer
 
Comments
Love Allah 2-Jan-18 16:47pm    
thank u very much it worked
Thomas Daniels 2-Jan-18 16:47pm    
You're welcome!
Maciej Los 2-Jan-18 17:26pm    
5ed!
Answer #1 by ProgramFOX[^] is very good. In addition to it, i'd like to share another solution which uses Linq.

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

//display 
foreach (var key in respectiveKeys)
{
	Console.WriteLine(key);
}
 
Share this answer
 
Comments
Thomas Daniels 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 Daniels 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 Daniels 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 :)

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