Solved

Dictionary - get key by value

Posted on 2014-10-10
3
4,257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 34

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 34

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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