Retrieve Dictionary key from value

Hi,
How to find  key from value in an DictionaryList.
I am using vb.net desktop.


Regards
RIASAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
One way using LINQ -
Dim matches = (From pair in dictionary 
			   Where pair.Value.Equals(someValue) 
			   Select pair.Key)

Open in new window

Proof of concept -
Imports System.Runtime.CompilerServices
Imports System.Text

Module Module1
	Sub Main()
		Dim dictionary = "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?".RemovePunctuationCharacters().Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries).GroupBy(Function(x) x, StringComparer.OrdinalIgnoreCase).Select(Function(pair) New With {.Key = pair.Key, .Value = pair.Count()}).ToDictionary(Function(pair) pair.Key, Function(pair) pair.Value)
		Dim occurances = New Integer() {7, 6, 5, 4}

		Dim matches = (From occurance In occurances
					   From pair In dictionary
					   Where pair.Value.Equals(occurance)
					   Select $"{pair.Key} appears {occurance} time{If(occurance <> 1, "s", "")}!!!")

		For Each match In matches
			Console.WriteLine(match)
		Next
		Console.ReadLine()
	End Sub
End Module

Module Extensions
	<Extension>
	Public Function RemovePunctuationCharacters(source As String) As String
		Dim sb = New StringBuilder()
		For Each c As Char In source
			If Char.IsPunctuation(c) AndAlso Not Char.IsWhiteSpace(c) Then Continue For
			sb.Append(c)
		Next
		Return sb.ToString()
	End Function
End Module

Open in new window

Which produces the following output -Capture.PNG-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance programmer / ConsultantCommented:
Not being a great fan of LINQ (but the above example is easy to understand) you could use a traditional method such as

For Each kvp As KeyValuePair(Of X, Y) In myDictionary
    if kvp.Value.Equals(ValueToSearchFor) then
       KeyToFindIs = kvp.Key
       return
    end if
Next kvp

ps.  Don't forget LINQ probably does something like this in the background, so the performance of each is likely to be pretty similar
RIASAuthor Commented:
Hello,
Thanks Saige!
Thanks Andy!!  Can you please guide me on how to select 2 solutions as the answer (best And assisted). At the moment it just allows me to select 1 solution.

Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.