Solved

Dictionary - get key by value

Posted on 2014-10-10
3
3,267 Views
Last Modified: 2014-10-10
Public Xdictionary As New Dictionary(Of String, Integer)
... add values...
.. ABC, 1000..
... DEF, 2935..
....Many Many Values...


Need to get the Key(string) by giving value in a loop.

Something like    
Dim fiaddr As FileInfo() = diaddr.GetFiles("*", SearchOption.TopDirectoryOnly)
For Each scannedFile As FileInfo In fiaddr
      If Xdictionary.ContainsValue(scannedFile.Name) Then
            msgbox ("The Key for file is : " &  Xdictionary.GETKEYFROMVALUE(scannedFile.Name))
      END IF
NEXT

Open in new window




Basically i need dictionary.ContainsValue to return the key
In vb
0
Comment
Question by:Ess Kay
  • 2
3 Comments
 
LVL 32

Assisted Solution

by:it_saige
it_saige earned 500 total points
ID: 40373463
Something like this will return your keys that match the value:
Dim keys = dict.Where(Function(pair) pair.Value = match).Select(Function(pair) pair.Key).ToList()
If keys.Count > 0 Then
	' Now you need to decide which matching key you want.
	' If you don't care, you can take the first
	key = keys(0)
End If

Open in new window

If your values are distinct, then you may be better off just selecting the first *match* as it were:
Dim keys = dict.Where(Function(pair) pair.Value = match).Select(Function(pair) pair.Key).Take(1).ToList()
If keys.Count > 0 Then
	key = keys(0)
End If

Open in new window

If you are using a dictionary that has keys which are a reference type, you could use this method:
Dim key = dict.Where(Function(pair) pair.Value = match).Select(Function(pair) pair.Key).FirstOrDefault()
If key IsNot Nothing Then
	'Do something with your key...
End If

Open in new window

Putting it all together:
Dim Xdictionary As New Dictionary(Of FileInfo, String)
Dim diaddr As DirectoryInfo
Dim fiaddr As FileInfo() = diaddr.GetFiles("*", SearchOption.TopDirectoryOnly)
For Each scannedFile As FileInfo In fiaddr
	Dim keys = Xdictionary.Where(Function(pair) pair.Value.Contains(scannedFile.Name)).Select(Function(pair) pair.Key).ToList()
	If keys.Count > 0 Then
		' Now you need to decide which matching key you want.
		' If you don't care, you can take the first
		' key = keys(0)
		MsgBox("The Key for file is : " & keys(0).ToString())
	End If
Next

Open in new window

-saige-
0
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
ID: 40373530
Example -
Imports System.IO

Module Module1
	Sub Main()
		Dim dictionary As New Dictionary(Of String, Integer)
		dictionary.Add("ABC", 123)
		dictionary.Add("DEF", 456)
		dictionary.Add("GHI", 789)
		dictionary.Add("JKL", 0)
		Dim keys = dictionary.Where(Function(pair) pair.Value = 0).Select(Function(pair) pair.Key).ToList()
		If keys.Count > 0 Then
			Console.WriteLine(String.Format("Value - 0, Is Keyed with {0}", keys(0)))
		End If

		keys = dictionary.Where(Function(pair) pair.Value = 123).Select(Function(pair) pair.Key).ToList()
		If keys.Count > 0 Then
			Console.WriteLine(String.Format("Value - 123, Is Keyed with {0}", keys(0)))
		End If

		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output:Capture.JPG-saige-
0
 
LVL 15

Author Closing Comment

by:Ess Kay
ID: 40373778
Awesome. Thanks.

I actually ended up switch the values and keys by creating the disctionary (of string, string)
because once the item successfully processes, i update the value to completed, from whatever was in it.


But your solution worked great before i made the change.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now