Solved

Find key in dictionary list

Posted on 2016-10-03
6
45 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
[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
  • 4
  • 2
6 Comments
 
LVL 51

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 51

Expert Comment

by:Rgonzo1971
ID: 41826109
Using Linq

Console.WriteLine (values.FirstOrDefault (x => x.Value == "Result").Key);
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

710 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