How to get Key from a Dictionary

Hi, and thanks. I've got a dictionary that is created with to store this information when its generated. Once this dictionary is created i want to asign the mision info to a button. How i can acces to this info from my asignedMision Dictionary?

 if (!haveMisionsAsigned && gameRecord.searchedArea == null)
        {
            for (int i = 0; i < areaArray.Length; i++)
            {
                if (gameRecord.availableAreas[areaArray[i]])
                {
                    gameRecord.misionsAsigned.Add(misionController.AssignMision("Busqueda", areaArray[i]), areaArray[i].localizations[Random.Range(0,areaArray[i].localizations.Length)].transform);
                }
            }
        }
        else if (!haveMisionsAsigned && gameRecord.searchedArea != null)
        {
            for (int i = 0; i < 3; i++)
            {
                gameRecord.misionsAsigned.Add(misionController.AssignMision(misionTypeArray[Random.Range(0, misionTypeArray.Length)], gameRecord.searchedArea), gameRecord.searchedArea.localizations[Random.Range(0,gameRecord.searchedArea.localizations.Length)].transform);
            }
        }
        if (!gameRecord.estaEnMarxaUnaMisio)
        {
           //Get the mision info from dictionary (like mision.misiondescription) to add it in a funcionality of a button
        }

May be there is other easiest way. In that case, tell it to me too please. Thanks

I'm a little bit too confused by your code style to make sense of what exactly you're trying to do here.

Do you need to loop through a dictionary? Or do you just want to get a single item with a known key?

public Dictionary<string, YourObject> myDict;

// Loop through each value
foreach (KeyValuePair<string, YourObject> item in myDict) {
    // Access the key with item.Key
    // Access the value with item.Value
}

// Find a specific item by key, using LINQ (ensure you add using statement to top of script)
using System.LINQ;
YourObject yourObj = myDict.Single(s => s.Key == "someValue").Value;

Is this what you're after?

1 Like

Do you need to get a key, or a value? As in Key/Value pair.

I think that the code Schneider21 told is what i was looking for, especially the foreach loop with being able to acces the key.
But I don't understand nothing about the 9-11 lines of the code.
Thanks a lot, and if you want and please, I would like to know the meaning of those lines.

Well I thought the comment spelled it out pretty clearly, but okay...

If you want to get a single item from a dictionary, using the key, line 11 is how you'd do that. This way uses the LINQ class, so the using statement from like 10 needs to be added to the top of your script with the other dependencies.

sorry... what i don't understand is the meaning of "=>" or what seems "s" variable are doing or tha Single.. is what confuse me.. but thanks anyway:)

That's a lambda expression. It's a bit more advanced, but very common in LINQ statements.

[quote=“Schneider21”, post:2, topic: 728926]
I’m a little bit too confused by your code style to make sense of what exactly you’re trying to do here.

Do you need to loop through a dictionary? Or do you just want to get a single item with a known key?

public Dictionary<string, YourObject> myDict;

// Loop through each value
foreach (KeyValuePair<string, YourObject> item in myDict) {
    // Access the key with item.Key
    // Access the value with item.Value
}

// Find a specific item by key, using LINQ (ensure you add using statement to top of script)
using System.LINQ;
YourObject yourObj = myDict.Single(s => s.Key == "someValue").Value;

Is this what you’re after?
[/quote]
This is exactly correct brother. But it is System.Linq. Thanks mate