-1

The dificulty I'm facing is as follows: I need to create a dictionary with something like 10 main definitions in it. What I actually need is to be able to recognize some X amount of strings that should represent one certain string. I want to have like 10 main strings and to be able to add different representative string to each one of them. Example: I have the strings "animal", "fruit" and "object" and I want to assing e.g. the strings "dog", "cat" and "snake" to the string "animal". The point is that everytime I face one of those strings, I'll want replace it with "animal". I imagine this as some kind of dictionary and I've read the documentary about this class in c#, but I'm not quite sure it's the best method so that's why I'm asking you. My idea was to create a new entry each time I face one of the substrings and to set that substring (e.g. "dog") as a key with value - the main string (in this case "animal"), but I find it quite inappropriate. Following question - could you suggest a good enough method to store the data from that "dictionary" locally/online, so that I can collect data troughout the time I'm using my code. Thanks a lot, friendly members of this community! :D

D. Petrov
  • 1,147
  • 15
  • 27
  • Adding paragraphs to your question is a good start. – Visual Vincent Apr 07 '16 at 19:44
  • each of those words are linked to one word only? (for example, "dog", "cat" and "snake" can only be linked to "animal". Or can "dog" and "cat" be linked also to "mammal" and "snake" to reptile?) – FinnTheHuman Apr 07 '16 at 19:46
  • 1
    But to point you in the right direction: **Storing multiple strings:** `List` -- **Finding a string:** `foreach`-loops/`.Contains()`/Linq. -- **Saving the Dictionary:** _Binary Serialization_. – Visual Vincent Apr 07 '16 at 19:46
  • @FinnTheHuman I only need them to be linked to one string. No mammals and reptiles. – D. Petrov Apr 07 '16 at 19:48

2 Answers2

0

If I understand correctly, you can just use a dictionary:

var d = new Dictionary();
d.Add("dog", "animal");
....


d["dog"]; //this gives you animal.

You can do this with each item you want to replace, and the dictionary will give you its replacement value.

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
0

What would be best in your case would be to inverse your logic. You should use a Dictionary with a string a key and List as value and retrieve the value using the key which is a member of your list.

var d = new Dictionary<string,List<string>>();
d.Add("Animal", new List("Dog","Cat");
d.FirstOrDefault(x => x.Value.Contains("Cat")).Key;
Kevin Avignon
  • 2,853
  • 3
  • 19
  • 40