The element operators are used to return a single, or a specific element from the sequence or collection. For example, in a school when we ask, who is the principal? Then there will be only one person that will be the principal of the school. So the number of students is a collection and the principal is the only result that comes from the collection.
The LINQ Standard Query Operator supports 8 types of element operators:
- ElementAt
- ElementAtOrDefault
- First
- FirstOrDefault
- Last
- LastOrDefault
- Single
- SingleOrDefault
ElementAt Operator
The ElementAt operator is used to return an element from the particular index from the given collection or sequence. Here the specified index is zero-based index. Suppose an array contains 3 elements, i.e, 1, 2, 3 and we want to print the value at index 1, so we use an ElementAt method because it will return the element present at index 1, i.e, 2.
Important Points:
- It does not support query syntax in C# and VB.Net languages.
- It support method syntax in both C# and VB.Net languages.
- It present in both the Queryable and Enumerable class.
- If the given index is out of range, then this method will throw an ArgumentOutOfRangeException.
Example 1:
// C# program to illustrate the // use of ElementAt operator using System;
using System.Linq;
class GFG {
// Main Method
static public void Main()
{
// Data source
string [] sequence = { "Dog" , "Cat" , "Cow" ,
"Goat" , "Parrot" };
// Display the sequences
Console.WriteLine( "Sequence is: " );
foreach ( var s in sequence)
{
Console.WriteLine(s);
}
// Get element at index 3
// Using ElementAt function
var result = sequence.ElementAt(3);
Console.WriteLine( "Element is: {0}" , result);
}
} |
Sequence is: Dog Cat Cow Goat Parrot Element is: Goat
Example 2:
// C# program to find the // ID of the employee using System;
using System.Linq;
using System.Collections.Generic;
// Employee details public class Employee {
public int emp_id
{
get ;
set ;
}
public string emp_name
{
get ;
set ;
}
public string emp_gender
{
get ;
set ;
}
public string emp_hire_date
{
get ;
set ;
}
public int emp_salary
{
get ;
set ;
}
} class GFG {
// Main method
static public void Main()
{
List<Employee> emp = new List<Employee>() {
new Employee() {emp_id = 209, emp_name = "Anjita" , emp_gender = "Female" ,
emp_hire_date = "12/3/2017" , emp_salary = 20000},
new Employee() {emp_id = 210, emp_name = "Soniya" , emp_gender = "Female" ,
emp_hire_date = "22/4/2018" , emp_salary = 30000},
new Employee() {emp_id = 211, emp_name = "Rohit" , emp_gender = "Male" ,
emp_hire_date = "3/5/2016" , emp_salary = 40000},
new Employee() {emp_id = 212, emp_name = "Supriya" , emp_gender = "Female" ,
emp_hire_date = "4/8/2017" , emp_salary = 40000},
new Employee() {emp_id = 213, emp_name = "Anil" , emp_gender = "Male" ,
emp_hire_date = "12/1/2016" , emp_salary = 40000},
new Employee() {emp_id = 214, emp_name = "Anju" , emp_gender = "Female" ,
emp_hire_date = "17/6/2015" , emp_salary = 50000},
};
// Query to find the ID of
// the employee at index 1
// Using ElementAt method
var res = emp.Select(e => e.emp_id).ElementAt(1);
Console.WriteLine( "Employee ID: {0}" , res);
}
} |
Employee ID: 210