Solved

Find key in dictionary list

Posted on 2016-10-03
6
50 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 52

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 52

Expert Comment

by:Rgonzo1971
ID: 41826109
Using Linq

Console.WriteLine (values.FirstOrDefault (x => x.Value == "Result").Key);
0
Stack Overflow Podcast - Frustrating Miracles

In this podcast, Stack Overflow interviewed Linux Academy CEO/Founder, Anthony James, and got his developer story!

"Follow your passion, be prepared to work hard and sacrifice, and, above all, don't let anyone limit your dreams."  - Donovan Bailey

 

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

What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

635 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