c sharp

C# LINQ ElementAt Operator

Language Integrated Query language (LINQ) is used to perform operations on the C# collections or Normal data structures. We can also perform the database query operations with LINQ. LINQ supports many methods and functions that are used for data manipulation like updating, deletion and insertion, etc.

LINQ ElementAt Operator

LINQ ElementAt() operator is used to search the element present in a data source. It searches the element based on the index value provided.

Syntax

input_source.ElementAt(index_number);

Where input_source is the data source that can be an array or list or any collection.

We will further understand this by the following examples.

As we know, indexing starts with 0.

Example 1

Here, we will create a list that has 10 integers and get the elements based on the index numbers provided inside the ElementAt().

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

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
       //create List named input_numbers
       var input_numbers = new  List() {100,200,300,456,12,34,56,78,54,44};
     
       //return 6th value  
        Console.WriteLine("Element present at 6th position: "+input_numbers.ElementAt(5));
       
        //return 9th value  
        Console.WriteLine("Element present at 9th position: "+input_numbers.ElementAt(8));
       
        //return 1st value  
        Console.WriteLine("Element present at 1st position: "+input_numbers.ElementAt(0));
       
        //return 4th value  
        Console.WriteLine("Element present at 4th position: "+input_numbers.ElementAt(3));
       
        //return 10th value  
        Console.WriteLine("Element present at 10th position: "+input_numbers.ElementAt(9));
       
    }
           
}

Output

Explanation
1. First, we created a list named input_numbers that holds 10 integer elements.

2. After that, we searched and displayed the following values using their index positions.

Example 2

Here, we will create a list that has 3 strings and get the elements based on the index numbers provided inside the ElementAt().

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

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
       //create List named input_strings
       var input_strings = new  List() {"Linuxhint","c#","vignan"};
     
       //return 1st value  
        Console.WriteLine("Element present at 1st  position: "+input_strings.ElementAt(0));
       
        //return 3rd value  
        Console.WriteLine("Element present at 3rd position: "+input_strings.ElementAt(2));
       
       
    }
           
}

Output

Explanation
1. First, we created a list named input_strings that holds 3 string elements.

2. After that, we searched and displayed the following strings using their index positions.

Conclusion

We learned how to return the element based on the index number using the ElementAt() operator available in C# – LINQ. We demonstrated two different examples to understand the concept better and make sure to use the modules – using System, using System.Linq, using System.Collections.Generic – 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