I have the following code, I am trying to get the strings which starts with capital, but I don’t know how! without linq I can do it but inside LINQ… no idea!
string[] queryValues1 = new string[10] {"zero", "one", "two", "three", "four", "five", "six", "seven","nine", "ten" };
string[] queryValues2 = new string[3] { "A", "b", "c" };
var queryResult =
from qResult in queryValues1
from qRes in queryValues2
where qResult.Length > 3
where qResult.Length < 5
where qRes[0].StartWithCapital //how to check if qRes started with a capital letter?
select qResult + "\t" + qRes + Environment.NewLine;
foreach (var qResult in queryResult)
{
textBox1.Text += qResult;
}
The earlier solutions here all assume
queryValues2consists 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:
(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 anIndexOutOfRangeExceptionon the""and aNullReferenceExceptionon thenull.Therefore, a safer alternative for the general case would be to use this: