LINQ First() Method

  Total Views : 205
  Comments : 0
 
  Keywords : How to use linq first() method in c# / vb.net with example, Linq first() extension method to get first element from list / collection in c# / vb.net with example, Linq first() method example

Here we will learn LINQ First() method in c#, vb.net with example and how to use LINQ First() in method syntax and query syntax to get first element from the list / collection in c#, vb.net with examples.

LINQ First() Method

In LINQ First() method / operator is used to return first element from the sequence of items in list / collection or first element in the sequence of items in list based on specified condition. In case if there no elements present in list / collection based on specified condition then LINQ First() method will throw an error.

Syntax of LINQ First() Method

Following is the syntax of using LINQ First() method to get first element from the list.

 

C# Code

 

int result = objList.First();

VB.NET Code

 

Dim result As Integer = objList.First()

If you observe above syntax we are getting first element from “objList” collection using LINQ First() method.

Example of LINQ First() in Method Syntax

Following is the example of using LINQ First() operator in method syntax to get first element from collection.

 

C# Code

 

using System;

using System.Linq;

 

namespace LINQExamples

{

class Program

{

static void Main(string[] args)

{

int[] objList = { 1, 2, 3, 4, 5 };

int result = objList.First();

Console.WriteLine("Element from the List: {0}", result);

Console.ReadLine();

}

}

}

VB.NET Code

 

Module Module1

Sub Main()

Dim objList As Integer() = {1, 2, 3, 4, 5}

Dim result As Integer = objList.First()

Console.WriteLine("Element from the List: {0}", result)

Console.ReadLine()

End Sub

End Module

If you observe above example we are getting first element from “objList” collection object by using LINQ First() operator in method syntax.

Output of LINQ First() in Method Syntax Example

Following is the result of LINQ First() operator in method syntax example.

 

First Element from the List 1

Example of LINQ First() in Query Syntax

Following is the example of using LINQ First() operator in query syntax to get first element from list.

 

C# Code

 

using System;

using System.Linq;

 

namespace LINQExamples

{

class Program

{

static void Main(string[] args)

{

int[] objList = { 1, 2, 3, 4, 5 };

int result = (from l in objList select l).First();

Console.WriteLine("Element from the List: {0}", result);

Console.ReadLine();

}

}

}

VB.NET Code

 

Module Module1

Sub Main()

Dim objList As Integer() = {1, 2, 3, 4, 5}

Dim result As Integer = (From l In objList).First()

Console.WriteLine("Element from the List: {0}", result)

Console.ReadLine()

End Sub

End Module

Output of LINQ First() in Query Syntax Example

Following is the result of LINQ First() operator in query syntax example.

 

Element from the List: 1

This is how we can use LINQ First() in method syntax and query syntax to get first element from the list / collection in c#, vb.net.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User