Solved

Dictionary - get key by value

Posted on 2014-10-10
3
3,662 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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