Get Dictionary key by value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?THVpZ2k=?=

    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

  • Marc Gravell

    #2
    Re: Get Dictionary key by value

    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.WriteLi ne("Found it: " + value.Value);
    }

    Marc

    Comment

    • Mark Rae [MVP]

      #3
      Re: Get Dictionary key by value

      "Luigi" <ciupazNoSpamGr azie@inwind.itw rote in message
      news:585E7C1B-FD1C-4641-AE1F-D6F3A84DA3F7@mi crosoft.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)?



      --
      Mark Rae
      ASP.NET MVP


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Get Dictionary key by value

        Luigi <ciupazNoSpamGr azie@inwind.itw rote:
        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 - <skeet@pobox.co m>
        Web site: http://www.pobox.com/~skeet
        Blog: http://www.msmvps.com/jon.skeet
        C# in Depth: http://csharpindepth.com

        Comment

        • OD

          #5
          Re: 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.
          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).FirstOrD efault();
          Console.WriteLi ne(key);

          --


          OD___



          Comment

          Working...