How to check if a string starts with a capital letter in a LINQ query

The earlier solutions here all assume queryValues2 consists of strings with at least one character in them. While that is true of the example code, it isn't necessarily always true.

Suppose, instead, you have this:

string[] queryValues2 = new string[5] { "A", "b", "c", "", null };

(which might be the case if the string array is passed in by a caller, for example).

A solution that goes straight for qRes[0] will raise an IndexOutOfRangeException on the "" and a NullReferenceException on the null.

Therefore, a safer alternative for the general case would be to use this:

where !string.IsNullOrEmpty(qRes) && char.IsUpper(qRes[0])

Try this:

where char.IsUpper(qRes[0])

Check Char.IsUpper(qRes[0]).

Tags:

C#

Linq