Search code examples
c#umbraco

Null check in where?


I have the following:

var tagId = "5288";
source.Where(p => p.GetPropertyValue<IEnumerable<string>>("picker").Contains(tagId));

This returns the error System.ArgumentNullException: Value cannot be null.

So some of the returned results, does not contain a picker value. How would I check for this, in the above statement?

It is a Umbraco Multinode treepicker that is the "picker" value.


Solution

  • If I understand correctly, the result of GetPropertyValue can be null if the picker value is not found. In that case, you can use a null conditional operator:

    source.Where(p => p.GetPropertyValue<IEnumerable<string>>("picker")?.Contains(tagId) == true);
    

    Note the ?. after GetPropertyValue. If that method returns null then it's not true, so those will not be included in the filtered objects.