469,352 Members | 2,154 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

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 20267
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Rory Campbell-Lange | last post: by
1 post views Thread by Mamatha | last post: by
6 posts views Thread by Tina I | last post: by
1 post views Thread by Jan Danielsson | last post: by
reply views Thread by Jon Slaughter | last post: by
8 posts views Thread by Bob Altman | last post: by
reply views Thread by Chris Rebert | last post: by
1 post views Thread by nvj944 | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.