The default for KeyValuePair

后端 未结 7 689
悲哀的现实
悲哀的现实 2020-12-04 10:01

I have an object of the type IEnumerable> keyValueList, I am using

 var getResult= keyValueList.SingleOrDefault()         


        
7条回答
  •  生来不讨喜
    2020-12-04 10:34

    I recommend more understanding way using extension method:

    public static class KeyValuePairExtensions
    {
        public static bool IsNull(this KeyValuePair pair)
        {
            return pair.Equals(new KeyValuePair());
        }
    }
    

    And then just use:

    var countries = new Dictionary
    {
        {"cz", "prague"},
        {"de", "berlin"}
    };
    
    var country = countries.FirstOrDefault(x => x.Key == "en");
    
    if(country.IsNull()){
    
    }
    

提交回复
热议问题