How to check if a variable is a default value of KeyValuePair?

0
6686
Asked By Keven Krok On

I have a bit of an awkward example but I am trying to extract a value from a dictionary if the key contains a substring. I have this part working, but I need to know when it does not find a match. The response from this request does not seem to accept a null. Here is the code that I am using to find a value from the dictionary.

var result = dictionaryOfItems.Where(x => x.Key.Contains(substring)).FirstOrDefault();

This does what I want, but there will not always be a match. Sometimes result will be empty and I don't know how to verify this.

if(result != null) //doesnt work
if(result != default(KeyValuePair<string, List<string>>) // does not work either

How do i check if a KeyValuePair object contains the default value that would be returned for a miss with the FirstOrDefault query?

1 Answer

Answered By Elli Mongillo On

I don't have an explanation as to why the second option you tried does not work as it is more or less the same thing as this. The following code should allow you to check whether a KeyValuePair item is the default value.

if (!result.Equals(default(KeyValuePair<string, List<string>>)))

 

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.