C # dictionary: several keys per value

I am looking for a way to get multiple keys with one value. Yes, I already used the search function, but most of the answers are in the opposite direction (a few values for each key), but I want the opposite.

The rationale for this is that I want to store several item identifiers (this is for the bot) on the "main" identifier, and throwing these several identifiers into the value of one is too slow to change (searching for one value => looping all the main identifiers and getting each value , and then checking for this identifier).

Example

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

The value search should return: Key 1-4, not 5

So, I'm looking for a way to make this easier - as I said above.

Does anyone know if this is possible and how to do it? Thanks in advance.

+6
source share
5 answers

Make the dictionary a different way and make the value a list of items.

if, for example, Value is a string, and Key 1-4 is an int, your dictionary might look something like this:

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

retrieving Value on theDictionary["Value"] will then return a list of integers containing 1, 2, 3, and 4.

Edit - Added an 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
source

Edit: Looking at your editing, it looks like you developed this Dictionary back ... your keys should match the values, not your values, to match the keys.

You can do something like create a dictionary that maps foreign keys to internal keys, and then use the internal key to index the 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]] .

+5
source

You can overestimate your problem. Keys must be unique in order to be useful for search operations. Values ​​do not have to be unique. Multiple keys can point to the same value without causing problems.

+3
source

1) The service is absolutely correct. If you search on anything but a key ... and if you try to extract anything other than the corresponding value ... then something is definitely wrong. All else being equal, you probably DO NOT want a dictionary.

2) Based on what you are saying, perhaps the best type of collection might be List. In particular, 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, which could be IDEAL for you:

+1
source

Assuming you have an initial dictionary (matching keys with values), you can use some Linq to convert it to an inverse dictionary without manually creating this inverse dictionary.

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

Select the highlighted originalValues from the source dictionary and use them as newKeys . Your newValues is the set of your originalKeys that appears on each originalValue / newKey .


Example: https://dotnetfiddle.net/dhwUSC

Given the source dictionary

 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} 
0
source

Source: https://habr.com/ru/post/954973/


All Articles