Solved

Dictionary - get key by value

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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

687 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