Solved

Find key in dictionary list

Posted on 2016-10-03
6
36 Views
Last Modified: 2016-10-03
Hello,
How to find a whether value exists in dictionarylist and if exists then get the key of it ?

is it  containsVALUE? or        .TryGetValue?

Cheers
0
Comment
Question by:RIAS
  • 4
  • 2
6 Comments
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 41826091
HI,

pls try
	Dictionary<string, string> values =
	    new Dictionary<string, string>();

	values.Add("test1", "found1");
	values.Add("Test2", "found2");
	if (values.TryGetValue("test1", out Result)) 
	{
	    Console.WriteLine(Result);
	}

Open in new window

0
 

Author Comment

by:RIAS
ID: 41826107
Rgonzo1971,
Cheers mate!

Is there a reverese vway to do it

Have value and get key ?
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 41826109
Using Linq

Console.WriteLine (values.FirstOrDefault (x => x.Value == "Result").Key);
0
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.

 

Author Comment

by:RIAS
ID: 41826111
I am using vb.net 2015
0
 

Author Comment

by:RIAS
ID: 41826123
Rgonzo1971,
Found your solution on some other expeerts exchange question:

Dim keys = reportdays.Where(Function(pair) pair.Value =  date1).Select(Function(pair) pair.Key).ToList()
If keys.Count > 0 Then
    Console.WriteLine(String.Format("key: {0}", keys(0)))
End If
0
 

Author Closing Comment

by:RIAS
ID: 41826124
Cheers!
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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 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