c sharp

C# LINQ First() Method

In this article, we will discuss how to return the first element using the LINQ First() function.

Language Integrated Query language (LINQ) is used to perform operations on the C# collections or normal data structures.

LINQ First()

The First() method in LINQ returns the first element from the specified data structure. There are two ways to use this method. Let’s look into it.

Approach 1: Using Method

We will use the normal First() method to return the first element.

Syntax

list.First()

Where the list is the List object created. 

Approach 2: Using Query

We will use the Query similar to SQL expression that returns the first element.

Syntax

fromin list select i.First()

Where the list is the List object created.

Example 1

Here, we will create a list that holds the integer elements and use the First() method to return the first value only.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
       //create list of integers
       List first_list = new List() {100,200,300,456};
       
       
       Console.WriteLine("List: ");
           foreach (var values in first_list)
            {
                Console.WriteLine(values);
            }
           
           
        //get the first element from the list
       var result = first_list.First();
           
        Console.WriteLine("First element in the List: "+result);
   
         
}
}

Output

Explanation
1. First, we created a list data structure with 4 integers.

2. After that, we applied the First() method to get the first element and display the result.

Example 2

Here, we will create a list that holds the string elements and use the First() method to return only the first element.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
       //create list of strings
       List first_list = new List() {"linuxhint","sravan","vignan","java"};
       
       
       Console.WriteLine("List: ");
           foreach (var values in first_list)
            {
                Console.WriteLine(values);
            }
           
           
        //get the first element from the list
       var result = first_list.First();
           
        Console.WriteLine("First element in the List: "+result);
   
         
}
}

Output

Explanation
1. First, we created a list data structure with 4 strings.

2. After that, we applied the First() method to get the first element and display it using the Console.Write() function.

Example 3

Here, we will create two lists that hold the integer and string elements separately. Get the first element from both lists using Query.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
        //create list of integers
       List first_list = new List() {100,200,300,456};
       
       //create list of strings
       List second_list = new List() {"linuxhint","sravan","vignan","java"};
       
       
       Console.WriteLine("First List: ");
           foreach (var values in first_list)
            {
                Console.WriteLine(values);
            }
           
           
        //get the first element from the list
       var result = (from i  in first_list select i).First();
           
        Console.WriteLine("First element in the List: "+result);
       
         Console.WriteLine("Second List: ");
           foreach (var values2 in second_list)
            {
                Console.WriteLine(values2);
            }
           
           
        //get the first element from the list
       var result2 = (from i  in second_list select i).First();
           
        Console.WriteLine("First element in the List: "+result2);
   
         
}
}

Output

Explanation
1. We created two Lists.

2. We returned the First element from both lists.

Conclusion

We came to the end of this chapter. The First() method in LINQ  returns only the first element from the data structure. Here, we used the List as a data structure. We implemented the LINQ First() method in two ways. Make sure to use and include the using System.Linq and using System.Collections.Generic command lines in your code.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain