Search This Blog

Tuesday, January 28, 2014

LINQ FirstOrDefault C# Example

C# > LINQ > FirstOrDefault

Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.

Example

string[] cars = { "audi", "ford", "mercedes", "dacia" };
string car = cars.FirstOrDefault(c => c == "ford"); //ford
car = cars.FirstOrDefault(c => c == "ford1"); //null