Dictionary, access the password. c # .net

0

I have a variable that stores a dictionary returned by a method, that is, the variable calls a method and it returns a dictionary.

var listofchamps = SAPI.GetChampions(RiotSharp.Misc.Region.euw, data).Champions.Values;

I attach the following image:

I could iterate all the integers returned with a foreach such that:

    foreach (var something in listofchamps)
    {
        if(something.Id == champ_id) { Console.WriteLine(something.Name); break; }
    }

However, I think it is not very efficient, because you need to load the key one by one, making a comparison between the id of the list and the current id by the user.

I was wondering how I could get the string directly, giving it the value such as:

listofchamps[champ_id];

in this way access the string without calling the foreach.

Thanks in advance.

    
asked by Omar 22.07.2018 в 14:39
source

1 answer

1

You can search the list with the Where method so:

IEnumerable<RiotSharp.StaticDataEndpoint.Champion.ChampionStatic> enumerableWhere = listofchamps.Where(champ => champ.Id == champ_id)

Then you should check if enumerableWhere has some element, for example with

enumerableWhere.Any()

since there may not be a champion with the ID you are looking for. Finally, if the ID is unique, enumerableWhere should contain a single element, you can access it with

enumerableWhere.First()
    
answered by 22.07.2018 / 14:54
source