Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Dictionary: Multiple KEYS per Value

Tags:

c#

.net

map

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.

like image 353
user1818486 Avatar asked Sep 30 '13 20:09

user1818486


4 Answers

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

like image 153
Andrew Coonce Avatar answered Sep 21 '22 19:09

Andrew Coonce


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.

like image 39
Dweeberly Avatar answered Sep 21 '22 19:09

Dweeberly


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"];
like image 29
MatteKarla Avatar answered Sep 20 '22 19:09

MatteKarla


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
like image 36
paulsm4 Avatar answered Sep 24 '22 19:09

paulsm4