SanSolo
SanSolo

Reputation: 2375

How to fetch key value pairs from IDictionary?

I have a jSon file which contains some values like this:

{AvailableWeapons:[ {"LevelNumber":1,"WeaponName":"Axe"},{"LevelNumber":5,"WeaponName":"WarHammer"} ]

Using miniJSon, I have parsed this into an IDictionary as follows:

IDictionary weaponsGrid = JsonManager.instance.availableWeapons as IDictionary;

I'm accessing this in a method that has an integer parameter "level". The method takes the parameter, compares it with each levelnumber field. If level is greater than levelNumber, the corresponding weapon is made available.

PseudoCode:

foreach levelNumber in weaponsGrid,
if currentLevel > levelNumber
 get name of weapon available at that level 

how can I accomplish this in C#? thanks

Upvotes: 1

Views: 858

Answers (6)

Arghya C
Arghya C

Reputation: 10068

You can do something like this

IDictionary<int, string> dict = new Dictionary<int, string> { { 1, "Axe" }, { 6, "WarHammer" }, { 2, "Knife" }, { 9, "Gun" } };

int level = 5; //input to method
List<string> powerfulWeapons = new List<string>(); //method output
foreach (var item in dict)
{
    if (item.Key > level)
        powerfulWeapons.Add(item.Value);
}
//powerfulWeapons = [ "WarHammer", "Gun" ]

Or, using Linq

var weapons = weaponsGrid.Where(w => w.Key > 5).Select(d => d.Value);

In case you want just one

string firstHeavyWeapon = string.Empty;
var firstHeavyWeaponEntry = weaponsGrid.FirstOrDefault(w => w.Key > 5);
if (!firstHeavyWeaponEntry.Equals(new KeyValuePair<int, string>()))
    firstHeavyWeapon = firstHeavyWeaponEntry.Value;

As suggested in comments, if you want first (minimum) Key matching the criteria, you can do this

var firstHeavyWeaponEntry = weaponsGrid
                                .OrderBy(w => w.Key)
                                .FirstOrDefault(w => w.Key > 5);

Upvotes: 2

Rahul
Rahul

Reputation: 77876

Then use the Keys property like

foreach(var levelNumber in weaponsGrid.Keys)
{
   // your code here    
}

You can access the value using the key

weaponsGrid[levelNumber]

Considering you have your dictionary set up

        IDictionary<int, string> weaponsGrid = new Dictionary<int, string> 
        { 
        { 1, "Axe" }, 
        { 6, "WarHammer" }, 
        { 2, "Knife" }, 
        { 9, "Gun" } 
        };

You can check if a key exists and then get it's value like

        string weapon = weaponsGrid.ContainsKey(6) ? weaponsGrid[6] : string.Empty;

Or, get all the values

        foreach (var item in weaponsGrid.Values)
        {
            Console.WriteLine(item);

        }

Upvotes: 1

Gelootn
Gelootn

Reputation: 601

Using linq:

IDictionary<int, string> dict = new Dictionary<int, string> { { 1, "Axe" }, { 6, "WarHammer" }, { 2, "Knife" }, { 9, "Gun" } };

var list = dict.Where(x => x.Key > 5).Select(x=> x.Value)
            .ToList();

this gives you a list of values where the key is larger the X

Upvotes: 4

Ashish Rajput
Ashish Rajput

Reputation: 1529

use the KeyValuePair

foreach (KeyValuePair<Int32,string> weapen in weaponsGrid )
  {
    if(weapen.key > value)
    {
            var weapenName = weapen.Value
    }

  }

Upvotes: 1

Stephen Ross
Stephen Ross

Reputation: 892

You can access the keys in your dictionary by using the IDictionary.Keys property which returns an ICollection of your keys. For your example you would do something like:

foreach (var levelNumber in weaponsGrid.Keys)
{
    if (currentLevel > levelNumber)
    {
        var weaponName = weaponsGrid[levelNumber].Name;
    }
}

Upvotes: 0

Rono
Rono

Reputation: 3361

foreach (var item in weaponsGrid.Keys)
  if (item > levelNumber) {
    string weapon = weaponsGrid[item];
  }

Upvotes: 1

Related Questions