473,395 Members | 1,675 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Get Dictionary key by value

Hi all,
having a dictionary of <int, string(C# 2.0) is there a way to retrieve the
key for a specific value (of type string)?
Obviously I populate the Dictionary with not duplicated strings.

Thanks a lot.
--
Luigi

Sep 22 '08 #1
4 20622
Probably the easiest option here is to loop over the pairs;

public static int? FindKey(
this IDictionary<int, stringlookup,
string value)
{
foreach (var pair in lookup)
{
if (pair.Value == value)
return pair.Key;
}
return null;
}

Which you can then call via:

Dictionary<int, stringlookup = new Dictionary<int,
string>(); // etc
int? value = lookup.FindKey("abc");
if (value.HasValue)
{
Console.WriteLine("Found it: " + value.Value);
}

Marc
Sep 22 '08 #2
"Luigi" <ci****************@inwind.itwrote in message
news:58**********************************@microsof t.com...
having a dictionary of <int, string(C# 2.0) is there a way to retrieve
the
key for a specific value (of type string)?
http://www.google.co.uk/search?hl=en...by+value&meta=
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Sep 22 '08 #3
Luigi <ci****************@inwind.itwrote:
having a dictionary of <int, string(C# 2.0) is there a way to retrieve the
key for a specific value (of type string)?
Obviously I populate the Dictionary with not duplicated strings.
No - a dictionary is a one way mapping. If you want to be able to map
both ways, you need two dictionaries.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 22 '08 #4
OD
Hi all,
having a dictionary of <int, string(C# 2.0) is there a way to retrieve the
key for a specific value (of type string)?
Obviously I populate the Dictionary with not duplicated strings.

Thanks a lot.
dictionnaries are "one way". you'll have to iterate or use a second
dictionnary.

You said you're using C# 2.0, so just to show you what C#3.0 can bring,
you can simplify your code using a Linq query like this (case
insensitive search of string "two" in a dictionnary "dic"
dictionnary<int,string>) :

var key = (from k in dic where string.Compare(k.Value, "two", true) ==
0 select k.Key).FirstOrDefault();
Console.WriteLine(key);

--
OD___
www.e-naxos.com
Sep 22 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rory Campbell-Lange | last post by:
I have a dictionary of images. I wish to sort the dictionary 'v' by a dictionary value using python 2.3. The dictionary value is the date attribute as shown here: v This attribute is an...
1
by: Mamatha | last post by:
Hi I have a mutithreading application in VB.NET. In one function i stored some data in dictionaries. i declared global value to store data in dictionaries. Every time dictionary pointer has...
7
by: Bill Woodruff | last post by:
I've found it's no problem to insert instances of named delegates as values into a generic dictionary of the form : private Dictionary<KeyType, DelegatemyDictionary = new Dictionary<KeyType,...
6
by: Tina I | last post by:
Hi everyone, I have a small, probably trivial even, problem. I have the following HTML: I need to make this into a dictionary like this: dictionary = {"METAR:" : "ENBR 270920Z 00000KT 9999...
1
by: Jan Danielsson | last post by:
Hello all, I'm sure I'm not using this right, but I don't understand what I'm doing wrong. What I want is to get all the cookies from the request, then extract the 'sessId' cookie. I'm using...
3
by: MortyContorty | last post by:
Hello: I am new to Python (and a programming greenie for that matter) and I was trying to figure out how to change the "value" of my dictionary dataset from a string to a float. I imagine it is...
0
by: Jon Slaughter | last post by:
How do I modify the value of a dictionary object? I have something like Dictionary<string, AQ = ... Where A is a struct. I want to change some values in A but
8
by: Bob Altman | last post by:
Hi all, I'm trying to do something that should be really easy, but I can't think of an obvious way to do it. I have a dictionary whose value is a "value type" (as opposed to a reference type --...
0
by: Chris Rebert | last post by:
On Thu, Oct 16, 2008 at 12:19 PM, John Townsend <jtownsen@adobe.comwrote: Right, this clobbers the existing entry with this new blank one. This is evidenced by the fact that you're performing an...
1
by: nvj944 | last post by:
I'm trying to subclass a dict object to create an object where some key values = 'property(fget, fset, fdel, doc)'. For some reason when I get the dictionary key a 'property object' is return instead...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.