Select Value of List of KeyValuePair

前端 未结 4 1275
滥情空心
滥情空心 2021-01-03 19:43

How can i select the value from the List of keyvaluepair based on checking the key value

List> myList = new         


        
4条回答
  •  独厮守ぢ
    2021-01-03 20:30

    If you're stuck with the List, you can use

    myList.First(kvp => kvp.Key == 5).Value
    

    Or if you want to use a dictionary (which might suit your needs better than the list as stated in the other answers) you convert your list to a dictionary easily:

    var dictionary = myList.ToDictionary(kvp => kvp.Key);
    var value = dictionary[5].Value;
    

提交回复
热议问题