Get Dictionary key by value

L

Luigi

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.
 
M

Marc Gravell

Probably the easiest option here is to loop over the pairs;

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

Which you can then call via:

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

Marc
 
J

Jon Skeet [C# MVP]

Luigi said:
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.
 
N

namdia

public int findMyValue(Dictionary<int,strnig> myDic, string val)
{
foreach(int key in myDic.Keys)
{
if ( myDic[key ] == val)
{
return key ;
}
}
return null;
}



ciupazNoSpamGrazi wrote:

Get Dictionary key by value
22-Sep-08

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

Previous Posts In This Thread:

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

Re: Get Dictionary key by value
http://www.google.co.uk/search?hl=e...252&q="C#"+Dictionary+find+key+by+value&meta=


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Re: Get Dictionary key by value
No - a dictionary is a one way mapping. If you want to be able to map
both ways, you need two dictionaries.

--
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth:

dictionnaries are "one way".
dictionnaries are "one way". you will have to iterate or use a second
dictionnary.

You said you are using C# 2.0, so just to show you what C#3.0 can bring,
you can simplify your code using a Linq que

Probably the easiest option here is to loop over the pairs; public
Probably the easiest option here is to loop over the pairs;

public static int? FindKey(
this IDictionary<int, string> lookup,
string value)
{
foreach (var pair in lookup)
{
if (pair.Value == value)
r

Get dictionary key by value
Assuming you have a "deletedMsgs" dictionary and you want to remove a message from the "failedPosts" queue, but you need the failedPosts key that matches the value found in deletedMsgs....

int keyFou

EggHeadCafe - Software Developer Portal of Choice
Programming C#
http://www.eggheadcafe.com/tutorials/aspnet/718b8580-cd75-4234-8764-db65bebbe8ad/programming-c.aspx
 
P

Peter Duniho

namdia said:
public int findMyValue(Dictionary<int,strnig> myDic, string val)
{
foreach(int key in myDic.Keys)
{
if ( myDic[key ] == val)
{
return key ;
}
}
return null;
}

That doesn't compile. Even if we fix the spelling error, that's not a
very efficient way to perform the search. Much better to just enumerate
the KeyValuePair<TKey, TValue> instances in the Dictionary<TKey,
TValue>, examining the Value property of each. That way, no additional
overhead of looking up the value by key has to be incurred.

And oddly enough, that more-efficient approach -- enumerating the pairs
-- is in fact that Marc Gravell recommended to the original poster, OVER
A YEAR AGO when the question was first asked. Why you're replying now,
with a suggestion that is less efficient, I'm a bit puzzled by.

Pete
 
S

Scott M.

Aside from the fact that you can't return null on a method that is not
marked as void, what's wrong with the code that you have?

-Scott
 
P

Peter Duniho

Jeff said:
Wait, what? Did you mean "int" instead of "void"?

That wouldn't have made sense either. If you want to return "null", you
have to be returning a nullable type. For example, "int?"

When presented with a poor code example, it can be difficult to
correctly find and describe _every_ problem with it. :) On the bright
side, it's unlikely that exact code example will ever make it into a
real program, even with the errors corrected.

Pete
 
J

Jeff Johnson

That wouldn't have made sense either. If you want to return "null", you
have to be returning a nullable type. For example, "int?"

I mentally made a second correction (removal of "not") that I failed to type
out. Here's what I was envisioning the statement should have been: "Aside
from the fact that you can't return null on a method that is marked as int."
 
P

Peter Duniho

Jeff said:
I mentally made a second correction (removal of "not") that I failed to type
out. Here's what I was envisioning the statement should have been: "Aside
from the fact that you can't return null on a method that is marked as int."

Yes, that makes sense too. :)
 
A

Ali Imam

You can get key using LINQ
like


Dim RelatedKeys As String = (
From kvp As KeyValuePair(Of String, String)
In dicFieldsName Where kvp.Value = sel.Trim() Select kvp).First().Key


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
On Monday, September 22, 2008 7:04 AM Jon Skeet [C# MVP] wrote:

No - a dictionary is a one way mapping. If you want to be able to map
both ways, you need two dictionaries.

--
Jon Skeet - <[email protected]>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
On Thursday, October 22, 2009 3:46 PM namdia wrote:
public int findMyValue(Dictionary<int,strnig> myDic, string val)
{
foreach(int key in myDic.Keys)
{
if ( myDic[key ] == val)
{
return key ;
}
}
return null;
}



ciupazNoSpamGrazi wrote:

Get Dictionary key by value
22-Sep-08

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

Previous Posts In This Thread:

ciupazNoSpamGrazi wrote:

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

Mark Rae [MVP] wrote:

Re: Get Dictionary key by value
http://www.google.co.uk/search?hl=e...252&q="C#"+Dictionary+find+key+by+value&meta=


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jon Skeet [C# MVP] wrote:

Re: Get Dictionary key by value
No - a dictionary is a one way mapping. If you want to be able to map
both ways, you need two dictionaries.

--
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth:

OD wrote:

dictionnaries are "one way".
dictionnaries are "one way". you will have to iterate or use a second
dictionnary.

You said you are using C# 2.0, so just to show you what C#3.0 can bring,
you can simplify your code using a Linq que

Marc Gravell wrote:

Probably the easiest option here is to loop over the pairs; public
Probably the easiest option here is to loop over the pairs;

public static int? FindKey(
this IDictionary<int, string> lookup,
string value)
{
foreach (var pair in lookup)
{
if (pair.Value == value)
r

Tom Jackson wrote:

Get dictionary key by value
Assuming you have a "deletedMsgs" dictionary and you want to remove a message from the "failedPosts" queue, but you need the failedPosts key that matches the value found in deletedMsgs....

int keyFou

EggHeadCafe - Software Developer Portal of Choice
Programming C#
http://www.eggheadcafe.com/tutorials/aspnet/718b8580-cd75-4234-8764-db65bebbe8ad/programming-c.aspx
 
A

Ali Imam

Using LINQ

Dim RelatedKeys As String = (From kvp As KeyValuePair(Of String, String) In dicFieldsName Where kvp.Value = sel.Trim() Select kvp).First().Key
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
On Monday, September 22, 2008 7:04 AM Jon Skeet [C# MVP] wrote:

No - a dictionary is a one way mapping. If you want to be able to map
both ways, you need two dictionaries.

--
Jon Skeet - <[email protected]>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
On Thursday, October 22, 2009 3:46 PM namdia wrote:
public int findMyValue(Dictionary<int,strnig> myDic, string val)
{
foreach(int key in myDic.Keys)
{
if ( myDic[key ] == val)
{
return key ;
}
}
return null;
}



ciupazNoSpamGrazi wrote:

Get Dictionary key by value
22-Sep-08

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

Previous Posts In This Thread:

ciupazNoSpamGrazi wrote:

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

Mark Rae [MVP] wrote:

Re: Get Dictionary key by value
http://www.google.co.uk/search?hl=e...252&q="C#"+Dictionary+find+key+by+value&meta=


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jon Skeet [C# MVP] wrote:

Re: Get Dictionary key by value
No - a dictionary is a one way mapping. If you want to be able to map
both ways, you need two dictionaries.

--
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth:

OD wrote:

dictionnaries are "one way".
dictionnaries are "one way". you will have to iterate or use a second
dictionnary.

You said you are using C# 2.0, so just to show you what C#3.0 can bring,
you can simplify your code using a Linq que

Marc Gravell wrote:

Probably the easiest option here is to loop over the pairs; public
Probably the easiest option here is to loop over the pairs;

public static int? FindKey(
this IDictionary<int, string> lookup,
string value)
{
foreach (var pair in lookup)
{
if (pair.Value == value)
r

Tom Jackson wrote:

Get dictionary key by value
Assuming you have a "deletedMsgs" dictionary and you want to remove a message from the "failedPosts" queue, but you need the failedPosts key that matches the value found in deletedMsgs....

int keyFou

EggHeadCafe - Software Developer Portal of Choice
Programming C#
http://www.eggheadcafe.com/tutorials/aspnet/718b8580-cd75-4234-8764-db65bebbe8ad/programming-c.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top