Solved

Find key in dictionary list

Posted on 2016-10-03
6
28 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 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
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
Comment Utility
Rgonzo1971,
Cheers mate!

Is there a reverese vway to do it

Have value and get key ?
0
 
LVL 48

Expert Comment

by:Rgonzo1971
Comment Utility
Using Linq

Console.WriteLine (values.FirstOrDefault (x => x.Value == "Result").Key);
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:RIAS
Comment Utility
I am using vb.net 2015
0
 

Author Comment

by:RIAS
Comment Utility
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
Comment Utility
Cheers!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now