C# Dictionary: Multiple KEYS per Value

2020-04-05 07:56发布

问题:

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.

回答1:

Do the dictionary the other way around and make the value a list of items.

if for example Value is a string and Key 1-4 are ints your dictionary could look something like:

var theDictionary = new Dictionary<string, List<int>>();

retrieving Value by theDictionary["Value"] would then return a list of ints containing 1, 2, 3 and 4.

Edit - Added example:

var theDictionary = new Dictionary<string, List<string>>
    {
        {"Value", new List<string> {"Key 1", "Key 2", "Key 3", "Key 4", "Key 5",}},
        {"Value2", new List<string> {"Key 5", "Key 2"}}
    };

var oneToFour = theDictionary["Value"];


回答2:

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



回答3:

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.



回答4:

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:

  • http://msdn.microsoft.com/en-us/library/system.collections.specialized.namevaluecollection.aspx


回答5:

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}


标签: c# .net map