0

When processing the elements of a JsonArray with linq, the signature is IEnumerable<JsonNode?>. So I was trying to come up with a clean filter that would only select the non-null elements. .Where(x => x!=null) does not work because the signature is still IEnumerable<JsonNode?> after that (resulting in warnings).

The following extension method does work, but is there a less clunky way to achieve the same?

public static class IEnumerableExtensions
{
    public static IEnumerable<T> NotNull<T>(this IEnumerable<T?> seq) where T : class 
    {
        foreach(var v in seq)
        {
            if(v != null) yield return v;
        }
    }
}
jmik
  • 311
  • 2
  • 10

0 Answers0