'C# Dictionary: Multiple KEYS per Value

I'm looking for a way to get multiple keys with a single value. Yes, I've already used the search function, but most answers are for the opposite direction (multiple values per key), but I want the opposite.

The reasoning behind this is that I want keep multiple Item-IDs (it's for a Bot) per "main" ID, and throwing those multiple IDs into a value of the one is too slow to modify (looking for one value => looping trough all main IDs & getting each value, then checking if that ID exists).

Example

Key 1 => Value
Key 2 => Value
Key 3 => Value
Key 4 => Value
Key 5 => Value 2

Looking for Value should return: Key 1-4, not 5

So I'm looking for a way to do that easier - like I said above.

Anyone knows if that's possible and how to do it? Thanks in advance.



Solution 1:[1]

Edit: Looking at your edit, it really looks like you have designed this Dictionary backwards... your keys should be for matching values, not your values for matching keys.

You could do something like create a dictionary that maps outer-keys to inner-keys, then use the inner-key to index a second dictionary.

Example:

var outer = new Dictionary<int, Guid> {
  { 1, GuidA },
  { 2, GuidA },
  { 3, GuidA },
  { 4, GuidA },
  { 5, GuidB }
};
var inner = new Dictionary<Guid, Value> {
  { GuidA, Value1 },
  { GuidB, Value2 }
};

You would access it as: value = outer[inner[key]].

Solution 2:[2]

You may be overthinking your problem. Keys need to be unique in order to be useful for lookup operations. Values do not need to be unique. Multiple keys can point to the same value without causing problems.

Solution 3:[3]

1) Servy is absolutely correct. If you're doing a search on anything but a key ... and if you're trying to retrieve anything but the corresponding value ... then something is definitely wrong. All things being equal, you probably DON'T want a dictionary.

2) Based on what you're saying, perhaps a better collection type might be a List. Specifically, a list of name/value pairs.

EXAMPLE:

List<string> NVList = new List<string>();
NVList.Add("color=blue");
...

3) Note that .Net has a specialized "NameValueCollection" class that might be IDEAL for you:

Solution 4:[4]

Assuming you have your initial dictionary (mapping your keys to values) already you can use some Linq to convert it into a reverse dictionary without having to create that reverse dictionary by hand.

var newDict = initialDict.Select(x=>x.Value).Distinct().ToDictionary(x=>x, x=> initialDict.Where(kvp=>kvp.Value == x).Select(kvp=>kvp.Key));

Select the distinct originalValues from your original dictionary and use those as your newKeys. Your newValues are the set of your originalKeys that mapped to each originalValue/newKey.


Example: https://dotnetfiddle.net/dhwUSC

Given an initial dictionary of

var initialDict = new Dictionary<int, string>{
        {1, "Value"},
        {2, "Value"},
        {3, "Value"},
        {4, "Value"},
        {5, "Value2"}
    };

the above function returns

Value: {1, 2, 3, 4}
Value2: {5}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Dweeberly
Solution 3 paulsm4
Solution 4