|
|
Enumerable.FirstOrDefault() returns the first element of a sequence, or a default value if the sequence contains no elements. For eg: Running the following query returns 'Jack'
C#
void Main()
{
List<string> list = new List<string>() { "Jack", "And", "Jill", "Went", "Up", "The", "Hill" };
var first = list.FirstOrDefault();
Console.WriteLine(first);
}
VB.NET
Private Sub Main()
Dim list As New List(Of String)(New String() {"Jack", "And", "Jill", "Went", "Up", "The", "Hill"})
Dim first = list.FirstOrDefault()
Console.WriteLine(first)
End Sub
Similarly if FirstOrDefault is used on an empty list of integers, it returns 0
'Like' us on our FaceBook page if you find this blog useful. Thanks!
Did you like this post?
|
|
|
||
|
|
|
|
Save on Delicious |
|
|
subscribe via rss |
|
subscribe via e-mail |
|
|
print this post |
|
follow me on twitter |




comments
0 Responses to "Return the First Element of a Sequence in LINQ"Post a Comment