I want to check if JArray.Children() is null in the foreach loop. I can do:
if (jArrayJson == null)
{
return;
}
but I want to do it in the foreach. This is the different things I have tried:
JArray jArrayJson = (JArray)token.SelectToken("text");
foreach (JToken item in jArrayJson?.Children() ?? Enumerable.Empty<T>()) // Wouldn't compile
foreach (JToken item in jArrayJson?.Children()) // This will fail and not stop a null value
I saw this post about exention method, but I could not integrate the metod: Check for null in foreach loop
public static IList<T> OrEmptyIfNull<T>(this IList<T> source)
{
return source ?? Array.Empty<T>();
}
foreach (JToken item in jArrayJson?.Children().OrEmptyIfNull()) // Wouldn't compile