2

I need to maintain a mapping between enum values, and a multi-value type (name+number). e.g.

  • ABC : {"john", 123}
  • DEF : {"paul", 234}
  • GHI : {"ringo", 345}

I planned to do this using a Dictionary of some type but strictly speaking not only the keys should be unique, but the values too - it's like a bi-directional dictionary.

Then I need to be able to write a method MyEnum lookup(string name, int number) which I'd planned would call .Single(x => x.Value.name == name && x.Value.number == number) But I'm struggling to find the right types and apparently the KeyValuePair returned cannot be null which is a problem if there is no match.

What is a good way to approach this?

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • 7
    Can you check here http://stackoverflow.com/questions/9438060/c-sharp-dictionary-type-with-unique-keys-and-values – Erkan Demirel Apr 07 '16 at 15:22
  • 2
    And Here, http://stackoverflow.com/questions/255341/getting-key-of-value-of-a-generic-dictionary/255638#255638 , This could be marked as duplicate. – Habib Apr 07 '16 at 15:22
  • The second part of my question remains but perhaps I should raise that as a separate question. – Mr. Boy Apr 07 '16 at 15:28
  • 1
    I'd consider using list of `Tuple`. Then you can simply run `list.Where(t => t.Item2=="John" && t.Item3=="123")` – rbm Apr 07 '16 at 15:29
  • But KeyValuePair can be nullable. Just declare the type as KeyValuePair?. – kent-id Apr 07 '16 at 15:52

0 Answers0