how to do a dictionary reverse lookup

frenchie

I have a dictionary of type <string, string> and for a particular case, I need to do a reverse lookup. So for instance suppose I have this entry <"SomeString", "ab"> and that I pass in "ab" then I would like to return "SomeString". Before I embark on a foreach loop over every entry in the dictionary, I was wondering what would be the most efficient way to do this reverse lookup?

Selman Genç

Basically, You can use LINQ and get the Key like this, without reversing anything:

var key = dictionary.FirstOrDefault(x => x.Value == "ab").Key;

If you really want to reverse your Dictionary, you can use an extension method like this:

public static Dictionary<TValue, TKey> Reverse<TKey, TValue>(this IDictionary<TKey, TValue> source)
{
     var dictionary = new Dictionary<TValue, TKey>();
     foreach (var entry in source)
     {
         if(!dictionary.ContainsKey(entry.Value))
             dictionary.Add(entry.Value, entry.Key);
     }
     return dictionary;
} 

Then you can use it like this:

var reversedDictionary = dictionary.Reverse();
var key = reversedDictionary["ab"];

Note: if you have duplicate values then this method will add the first Value and ignore the others.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to do reverse geocoding on WinJS

From Dev

How do i reverse this calculation?

From Dev

How to Reverse Lookup in Redis?

From Dev

Play Framework 2.x: How do i make a HTTP-GET for an reverse lookup (nominatim OSM)?

From Dev

Enum reverse lookup

From Dev

Ruby TCPServer always delay on dns reverse lookup? - how to disable?

From Dev

Reverse lookup object with array

From Dev

Modify reverse lookup with forward lookup

From Dev

how to access lookup dictionary from check_auth

From Dev

Pandas idiomatic way to do a dictionary lookup

From Dev

How to create multidimensional dictionary of unknown depth based on lookup dictionary? (python)

From Dev

How to reverse a dictionary (whose values are lists) in Python?

From Dev

How do I lookup an array constructor MethodHandle with MethodHandles.Lookup?

From Dev

LibXML2: how do I reverse lookup a XPath expression from a node

From Dev

How to safely reverse dns lookup on ip in a shell script

From Dev

Hashmap reverse key lookup

From Dev

Django reverse lookup

From Dev

Enum reverse lookup

From Dev

C++ - How to Make Static Dictionary to Lookup Matrix

From Dev

reverse lookup private zone

From Dev

How do I Reverse IP lookup for all hosted domains using same IP with linux centos 6-64 or cmd?

From Dev

How can I reverse lookup a localized string

From Dev

Pandas idiomatic way to do a dictionary lookup

From Dev

In Windows 10, is there a way to do a "reverse lookup" for a folder junction?

From Dev

Timeout on reverse lookup of 0.0.0.0

From Dev

How do I do a reverse DNS lookup from IP address to domain name?

From Dev

MongoDB: How to realize an lookup dictionary for checking text

From Dev

How to do reverse split

From Dev

Reverse lookup in django

Related Related

  1. 1

    How to do reverse geocoding on WinJS

  2. 2

    How do i reverse this calculation?

  3. 3

    How to Reverse Lookup in Redis?

  4. 4

    Play Framework 2.x: How do i make a HTTP-GET for an reverse lookup (nominatim OSM)?

  5. 5

    Enum reverse lookup

  6. 6

    Ruby TCPServer always delay on dns reverse lookup? - how to disable?

  7. 7

    Reverse lookup object with array

  8. 8

    Modify reverse lookup with forward lookup

  9. 9

    how to access lookup dictionary from check_auth

  10. 10

    Pandas idiomatic way to do a dictionary lookup

  11. 11

    How to create multidimensional dictionary of unknown depth based on lookup dictionary? (python)

  12. 12

    How to reverse a dictionary (whose values are lists) in Python?

  13. 13

    How do I lookup an array constructor MethodHandle with MethodHandles.Lookup?

  14. 14

    LibXML2: how do I reverse lookup a XPath expression from a node

  15. 15

    How to safely reverse dns lookup on ip in a shell script

  16. 16

    Hashmap reverse key lookup

  17. 17

    Django reverse lookup

  18. 18

    Enum reverse lookup

  19. 19

    C++ - How to Make Static Dictionary to Lookup Matrix

  20. 20

    reverse lookup private zone

  21. 21

    How do I Reverse IP lookup for all hosted domains using same IP with linux centos 6-64 or cmd?

  22. 22

    How can I reverse lookup a localized string

  23. 23

    Pandas idiomatic way to do a dictionary lookup

  24. 24

    In Windows 10, is there a way to do a "reverse lookup" for a folder junction?

  25. 25

    Timeout on reverse lookup of 0.0.0.0

  26. 26

    How do I do a reverse DNS lookup from IP address to domain name?

  27. 27

    MongoDB: How to realize an lookup dictionary for checking text

  28. 28

    How to do reverse split

  29. 29

    Reverse lookup in django

HotTag

Archive