Solved

Dictionary - get key by value

Posted on 2014-10-10
3
4,063 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 33

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 33

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.

713 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