Home > Software design >  C# Get Values from Dictionary array inside
C# Get Values from Dictionary array inside

Time:05-27

So today am trying to sort out how to get array values inside dictionary based on array but its picked randomly (see code below). Hope its understandable. I can't find anything about on google and also I can't find solution by myself. For any more information to provide please comment.

Dictionary Array looks like:

protected Dictionary<string, int>[] quests = new[]
{
  new Dictionary<string, int>() {{ "Name", 1 }, {"Description", 1 }, {"Earn", 500 }},
  new Dictionary<string, int>() {{ "Name", 2 }, {"Description", 2 }, {"Earn", 1000 }},
  new Dictionary<string, int>() {{ "Name", 3 }, {"Description", 3 }, {"Earn", 1200 }},
  new Dictionary<string, int>() {{ "Name", 4 }, {"Description", 4 }, {"Earn", 1400 }},
  new Dictionary<string, int>() {{ "Name", 5 }, {"Description", 5 }, {"Earn", 1600 }},
  new Dictionary<string, int>() {{ "Name", 6 }, {"Description", 6 }, {"Earn", 1800 }},
  new Dictionary<string, int>() {{ "Name", 7 }, {"Description", 7 }, {"Earn", 2000 }},
  new Dictionary<string, int>() {{ "Name", 8 }, {"Description", 8 }, {"Earn", 2200 }}
};

so inside is another dictionary. To get random from quest dictionary am using:

            Random random = new Random();
            int index = random.Next(quests.Length);

now how do I get those values from dictionary inside quest?

CodePudding user response:

You just need to take the right dictionary using the index and than fetch your data.

Follow this example that use Linq

var myQuest = quests [index];
var name = myQuest.FirstOrDefault(x=> x.Key == "Name").Value;
var description= myQuest.FirstOrDefault(x=> x.Key == "Description").Value;
var earn= myQuest.FirstOrDefault(x=> x.Key == "Earn").Value;

CodePudding user response:

By specifying an index to the array, and accessing the value by key in square brackets. Example:

var myQuest = quests[index];
var name = myQuest["Name"];
var description= myQuest["Description"];
var earn= myQuest["Earn"];
Console.WriteLine ("Hello Mono World:" name ":" description ":" earn);

Live demo

CodePudding user response:

Your example does not make too much of a sense in my opinion. The name and description should be directly connected to the dictionary entry. You just have triples of disconnected data. If you want to have three attributes for one dictionary entry you either create a class or use tuples if you want it without any more coding:

Class

  Dictionary<string, MyClass>

Tuples

  Dictionary<string, Tuple<string, string, int>>

Tuple example (class would be very similar)

        Dictionary<string, Tuple<string, string, int>> myDic = new Dictionary<string, Tuple<string, string, int>>();
        myDic.Add("MyItem1", new Tuple<string, string, int>("BagOfHope", "Valuable Item", 100));
        myDic.Add("MyItem2", new Tuple<string, string, int>("ShoeOfLuck", "Very Valuable Item", 200));
        myDic.Add("MyItem3", new Tuple<string, string, int>("Purse of sadness", "Trash Item", 10));

Get: Value for "MyItem2"

        Tuple<string, string, int> searchedItem;
        myDic.TryGetValue("MyItem2",  out searchedItem);

        int searchedvalue = searchedItem.Item3;

or even easier:

        myDic.FirstOrDefault(i => i.Key.Equals("MyItem2"));

Get: Tuple for items that have 100 in value

        myDic.Where(i => i.Value.Item3 == 100);
  • Related