I have a statement that checks if any element in a string array contains Error text value.
bool containsError = myStringArray.Contains("Error", StringComparer.InvariantCultureIgnoreCase);
How can I add another condition to also check if any element isNullOrEmpty?
>Solution :
One way is to add a condition with Any():
bool containsErrorOrEmptyElement = myStringArray.Contains("Error", StringComparer.InvariantCultureIgnoreCase)
|| myStringArray.Any(s => string.IsNullOrWhiteSpace(s));
I used string.IsNullOrWhiteSpace after assuming it better fits your requirements; you can also use string.IsNullOrEmpty instead.
You can also use LINQ’s Any for both conditions, by using the string.Equals method for case-insensitive comparison:
bool containsError = myStringArray.Any(s => string.IsNullOrWhiteSpace(s)
|| s.Equals("Error", StringComparison.InvariantCultureIgnoreCase));