Select Value of List of KeyValuePair

前端 未结 4 1277
滥情空心
滥情空心 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条回答
  •  梦毁少年i
    2021-01-03 20:12

    NOTE

    The generic Dictionary class, introduced in .NET 2.0, uses KeyValuePair.

    ITs better you make use of

    Dictionary.ICollection>
    

    and use ContainsKey Method to check the the key is there or not ..

    Example :

    ICollection> openWith =
                new Dictionary();
    openWith.Add(new KeyValuePair("txt", "notepad.exe"));
    openWith.Add(new KeyValuePair("bmp", "paint.exe"));
    openWith.Add(new KeyValuePair("dib", "paint.exe"));
    openWith.Add(new KeyValuePair("rtf", "wordpad.exe"));
    
    if (!openWith.ContainsKey("txt"))
    {
           Console.WriteLine("Contains Given key");
    }
    

    EDIT

    To get value

    string value = "";
    if (openWith.TryGetValue("tif", out value))
    {
        Console.WriteLine("For key = \"tif\", value = {0}.", value);
        //in you case 
       //var list= dict.Values.ToList(); 
    }
    

    in your caseu it will be

    var list= dict.Values.ToList(); 
    

提交回复
热议问题