score:14

Accepted answer

Try:

strings.FirstOrDefault(s=>s.StartsWith("J"));

And also if you are new to LINQ I'd recommend going through 101 LINQ Samples in C#.

score:2

bool hasJName = strings.Any(x => x.StartsWith("J"));

This checks to see if any names that start with J exist.

string jName = strings.FirstOrDefault(x => x.StartsWith("J"));

This returns the first name that starts with J. If no names starting with J are found, it returns null.

score:2

Using the First LINQ method (in System.Linq):

strings.First(e => e.StartsWith("J"));

Or FirstOrDefault if you are not sure that any element in your list will satisfy the condition:

strings.FirstOrDefault(e => e.StartsWith("J"));

Then, it returns null if no element has been found.

score:7

You can use FirstOrDefault:

var firstMatch = strings.FirstOrDefault(s => s.StartsWith("J"));
if(firstMatch != null)
{
    Console.WriteLine(firstMatch);
}

demo


Related Query

More Query from same tag