Element Operators in LINQ



Except the DefaultIfEmpty, all the rest eight standard query element operators return a single element from a collection.

Operator Description C# Query Expression Syntax VB Query Expression Syntax
ElementAt Returns an element present within a specific index in a collection Not Applicable Not Applicable
ElementAtOrDefault Same as ElementAt except of the fact that it also returns a default value in case the specific index is out of range Not Applicable Not Applicable
First Retrieves the first element within a collection or the first element satisfying a specific condition Not Applicable Not Applicable
FirstOrDefault Same as First except the fact that it also returns a default value in case there is no existence of such elements Not Applicable Not Applicable
Last Retrieves the last element present in a collection or the last element satisfying a specific condition Not Applicable Not Applicable
LastOrDefault Same as Last except the fact that it also returns a default value in case there is no existence of any such element Not Applicable Not Applicable
Single Returns the lone element of a collection or the lone element that satisfy a certain condition Not Applicable Not Applicable
SingleOrDefault Same as Single except that it also returns a default value if there is no existence of any such lone element Not Applicable Not Applicable
DefaultIfEmpty Returns a default value if the collection or list is empty or null Not Applicable Not Applicable

Example of ElementAt - Enumerable.ElementAt Method

C#

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

namespace Operators {
   class Program {
      static void Main(string[] args) {
      
         string[] names = { "Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow", 
                       "Hedlund, Magnus", "Ito, Shu" };
         Random random = new Random(DateTime.Now.Millisecond);

         string name = names.ElementAt(random.Next(0, names.Length));

         Console.WriteLine("The name chosen at random is '{0}'.", name);
         Console.ReadLine();
      }
   }
}

VB

Module Module1

   Sub Main()
   
      Dim names() As String = _{"Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow", 
	                     "Hedlund, Magnus", "Ito, Shu"}

      Dim random As Random = New Random(DateTime.Now.Millisecond)

      Dim name As String = names.ElementAt(random.Next(0, names.Length))

      MsgBox("The name chosen at random is " & name)
	  
   End Sub
   
End Module

When the above code of C# or VB is compiled and executed, it produces the following result −

The name chosen at random is Ito, Shu

Note − Here, the above output will change dynamically and names will be chosen randomly.

Example of First - Enumerable.First Method

C#

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

namespace Operators {
   class Program {
      static void Main(string[] args) {
      
         int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54, 83, 23, 87, 435, 67, 12, 19 };

         int first = numbers.First();

         Console.WriteLine(first);
         Console.ReadLine();
      }
   }
}

VB

Module Module1

   Sub Main()
   
      Dim numbers() As Integer = _{9, 34, 65, 92, 87, 435, 3, 54, 83, 23, 87, 435, 67, 12, 19}
      
      Dim first As Integer = numbers.First()

      MsgBox(first)
	  
   End Sub
   
End Module

When the above code of C# or VB is compiled and executed, it produces the following result −

9

Example of Last - Enumerable.Last Method

C#

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

namespace Operators {
   class Program {
      static void Main(string[] args) {
      
         int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54, 83, 23, 87, 435, 67, 12, 19 };

         int last = numbers.Last();

         Console.WriteLine(last);
         Console.ReadLine();
      }
   }
}

VB

Module Module1

   Sub Main()

      Dim numbers() As Integer = _{9, 34, 65, 92, 87, 435, 3, 54, 83, 23, 87, 435, 67, 12, 19};
      
      Dim last As Integer = numbers.Last()

      MsgBox(last)
	  
   End Sub
   
End Module

When the above code of C# or VB is compiled and executed, it produces the following result −

19
linq_query_operators.htm
Advertisements