Get key from value in a dictonary

Hi,
I have dictornary dict(key,value)=[{‘1’:‘a’},{‘2’:‘a’},{‘3’:‘a’},{‘4’:‘b’}]
i want to get the key from the value if pass from input?

Can anyone please help me out?

lets assume you are looking for a you will get multiple keys: 1,2,3

Try Following LINQ:
Assign activity:
lefttSide: arrKeys
Right side: yourDictVar.Where(Function (x) x.Value = YourSearchValOfSameDataType).Select(Function (x) x.Key).toArray

right i will get 3 keys but i want to take only first key and do some operation and remove that key from the dict so in the next iteration i have only two values…how can do that

give a try on:
grafik
YourdictVar.Where(Function (x) x.Value = YourSearchVar).Select(Function (x) x.Key).DefaultIfEmpty(-1).First()

it is handling the case no key is found and returns -1 for this

Hi,

You can use:

myKeyValuePair = yourDic.FirstOrDefault(Function(pair) pair.Value = value)

myKeyValuePair.Key returns your key.

If you want remove it, just write:

yourDic.Remove(myKeyValuePair.Key)

myKeyValuePair is of type KeyValuePair<string, string>

If nothing is found, myKeyValuePair will return Nothing.

1 Like