LINQ Element Operators

In LINQ, Element Operators are helpful to find the first or the last element of a list/collection and can also be used to fetch an element based on its index value.

The following are the different types of element operators available in LINQ.

Topics

  • First
  • FirstOrDefault
  • Last
  • LastOrDefault
  • ElementAt
  • ElementAtOrDefault
  • Single
  • SingleOrDefault

LINQ First Operator

First Operator can be used to fetch the first element from a list/collection.

outList=my_Datasource.First()

LINQ First Operator Example using C#

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

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] my_Items = { "Morning", "Afternoon", "Evening", "Night"};
            //Trying to fetch the first element of the list...
            String result = my_Items.First();
            
            Console.WriteLine(result);
            Console.ReadLine(); 
        }
    }
}
Output:
Morning

LINQ First Operator Example using Vb.Net

Module Module1
  Sub Main()
  Dim my_List As String() = {"Morning", "Afternoon", "Evening", "Night"}
  'Trying to fetch the first element of the list...
  Dim result As String = my_List.First()
  
  Console.WriteLine(result)
  Console.ReadLine()
  End Sub
End Module
Output:
Morning

LINQ FirstOrDefault Operator

FirstOrdefault Operator can be used to fetch the first element from a list/collection and in case if there are no values in that list/collection it will return a default value.

Example:
Int default value is 0
Boolean default value is False

outList=my_Datasource.FirstOrDefault()

LINQ FirstOrDefault Operator Example using C#

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

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32[] num_Items1 = {10, 20, 30, 40, 50};
            Int32[] num_Items2 = { };
            //Trying to fetch the first element of the list...
            Int32 result1 = num_Items1.FirstOrDefault();
            Int32 result2 = num_Items2.FirstOrDefault();
            
            Console.WriteLine("Result1 : {0}", result1);
            Console.WriteLine("Result2 : {0}", result2);
            Console.ReadLine();
        }
    }
}
Output:
Result1 : 10
Result2 : 0

LINQ FirstOrDefault Operator Example using Vb.Net

Module Module1
  Sub Main()
  Dim num_Items1 As Int32() = {10, 20, 30, 40, 50}
  Dim num_Items2 As Int32() = {}
  'Trying to fetch the first element of the list...
  Dim result1 As String = num_Items1.FirstOrDefault()
  Dim result2 As String = num_Items2.FirstOrDefault()
  
  Console.WriteLine("Result1 : {0}", result1)
  Console.WriteLine("Result2 : {0}", result2)
  Console.ReadLine()
  End Sub
End Module
Output:
Result1 : 10
Result2 : 0

LINQ Last Operator

Last Operator can be used to fetch the last element from a list/collection.

outList=my_Datasource.Last()

LINQ Last Operator Example using C#

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

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] my_Items = { "Morning", "Afternoon", "Evening", "Night"};
            //Trying to fetch the last element of the list...
            String result = my_Items.Last();
            
            Console.WriteLine(result);
            Console.ReadLine(); 
        }
    }
}
Output:
Night

LINQ Last Operator Example using Vb.Net

Module Module1
  Sub Main()
  Dim my_List As String() = {"Morning", "Afternoon", "Evening", "Night"}
  'Trying to fetch the last element of the list...
  Dim result As String = my_List.Last()
  
  Console.WriteLine(result)
  Console.ReadLine()
  End Sub
End Module
Output:
Night

LINQ LastOrDefault Operator

LastOrdefault Operator can be used to fetch the last element from a list/collection and in case if there are no values in that list/collection it will return a default value.

Example:
Int default value is 0
Boolean default value is False

outList=my_Datasource.LastOrDefault()

LINQ LastOrDefault Operator Example using C#

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

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32[] num_Items1 = {10, 20, 30, 40, 50};
            Int32[] num_Items2 = { };
            //Trying to fetch the last element of the list...
            Int32 result1 = num_Items1.LastOrDefault();
            Int32 result2 = num_Items2.LastOrDefault();
            
            Console.WriteLine("Result1 : {0}", result1);
            Console.WriteLine("Result2 : {0}", result2);
            Console.ReadLine();
        }
    }
}
Output:
Result1 : 50
Result2 : 0

LINQ LastOrDefault Operator Example using Vb.Net

Module Module1
  Sub Main()
  Dim num_Items1 As Int32() = {10, 20, 30, 40, 50}
  Dim num_Items2 As Int32() = {}
  'Trying to fetch the last element of the list...
  Dim result1 As String = num_Items1.LastOrDefault()
  Dim result2 As String = num_Items2.LastOrDefault()
  
  Console.WriteLine("Result1 : {0}", result1)
  Console.WriteLine("Result2 : {0}", result2)
  Console.ReadLine()
  End Sub
End Module
Output:
Result1 : 50
Result2 : 0

LINQ ElementAt Operator

ElementAt Operator can be used to fetch an element from a list/collection based on the specified index.

In the list datatype, the index starts from zero.

outList=my_Datasource.ElementAt()

LINQ ElementAt Operator Example using C#

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

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] my_Items = { "Morning", "Afternoon", "Evening", "Night"};
            //Trying to fetch the element of the list based on index number...
            String result = my_Items.ElementAt(1);
            
            Console.WriteLine(result);
            Console.ReadLine(); 
        }
    }
}
Output:
Afternoon

LINQ ElementAt Operator Example using Vb.Net

Module Module1
  Sub Main()
  Dim my_List As String() = {"Morning", "Afternoon", "Evening", "Night"}
  'Trying to fetch the element of the list based on index number...
  Dim result As String = my_List.ElementAt(1)
  
  Console.WriteLine(result)
  Console.ReadLine()
  End Sub
End Module
Output:
Afternoon

LINQ ElementAtOrDefault Operator

ElementAtOrdefault Operator can be used to fetch an element from a list/collection based on the specified index.and in case if that index is not valid, it will return a default value.

Example:
Int default value is 0
Boolean default value is False

outList=my_Datasource.ElementAtOrDefault()

LINQ ElementAtOrDefault Operator Example using C#

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

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32[] num_Items1 = {10, 20, 30, 40, 50};
            Int32[] num_Items2 = { };
            //Trying to fetch the element of the list based on index number...
            Int32 result1 = num_Items1.ElementAtOrDefault(1);
            Int32 result2 = num_Items2.ElementatOrDefault(1);
            
            Console.WriteLine("Result1 : {0}", result1);
            Console.WriteLine("Result2 : {0}", result2);
            Console.ReadLine();
        }
    }
}
Output:
Result1 : 20
Result2 : 0

LINQ ElementAtOrDefault Operator Example using Vb.Net

Module Module1
  Sub Main()
  Dim num_Items1 As Int32() = {10, 20, 30, 40, 50}
  Dim num_Items2 As Int32() = {}
  'Trying to fetch the element of the list based on index number...
  Dim result1 As String = num_Items1.ElementAtOrDefault()
  Dim result2 As String = num_Items2.ElementAtOrDefault()
  
  Console.WriteLine("Result1 : {0}", result1)
  Console.WriteLine("Result2 : {0}", result2)
  Console.ReadLine()
  End Sub
End Module
Output:
Result1 : 20
Result2 : 0

LINQ Single Operator

Single Operator can be used to fetch a single element from a list/collection that actually satisfies a given condition

If the collection has got more than one element in it or no elements in it, then it will throw an exception.

outList=my_Datasource.Single()

LINQ Single Operator Example using C#

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

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] my_Items = { "Morning", "Afternoon", "Evening", "Night"};
            //Trying to fetch the single element of the list...
            String result = my_Items.Where(x=>x.Contains("Ni")).Single();
            
            Console.WriteLine(result);
            Console.ReadLine(); 
        }
    }
}
Output:
Night

LINQ Single Operator Example using Vb.Net

Module Module1
  Sub Main()
  Dim my_List As String() = {"Morning", "Afternoon", "Evening", "Night"}
  'Trying to fetch the single element of the list...
  Dim result As String = my_Lists.Where(Function(x) x.Contains("Ni")).Single()
  
  Console.WriteLine(result)
  Console.ReadLine()
  End Sub
End Module
Output:
Night

LINQ SingleOrDefault Operator

SingleOrDefault Operator can be used to fetch a single element from a list/collection that actually satisfies a given condition

If the collection has got more than one element in it or no elements in it, then it will return a default value.

Example:
Int default value is 0
Boolean default value is False

outList=my_Datasource.SingleOrDefault()

LINQ SingleOrDefault Operator Example using C#

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

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32[] num_Items1 = {10, 20, 30, 40, 50};
            Int32[] num_Items2 = { };
            //Trying to fetch a single value from the list...
            Int32 result1 = num_Items1.Where(x=>x>40).SingleOrDefault();
            Int32 result2 = num_Items2.Where(x=>x>50).SingleorDefault();
            
            Console.WriteLine("Result1 : {0}", result1);
            Console.WriteLine("Result2 : {0}", result2);
            Console.ReadLine();
        }
    }
}
Output:
Result1 : 50
Result2 : 0

LINQ SingleOrDefault Operator Example using Vb.Net

Module Module1
  Sub Main()
  Dim num_Items1 As Int32() = {10, 20, 30, 40, 50}
  Dim num_Items2 As Int32() = {}
  'Trying to fetch a single value from the list...
  Dim result1 As String = num_Items1.Where(Function(x) x > 40).SingleOrDefault()
  Dim result2 As String = num_Items2.Where(Function(x) x > 50).SingleorDefault()
  
  Console.WriteLine("Result1 : {0}", result1)
  Console.WriteLine("Result2 : {0}", result2)
  Console.ReadLine()
  End Sub
End Module
Output:
Result1 : 50
Result2 : 0

Previous topic: LINQ Set Operator
Next Topic: Posting Soon

For more articles on LINQ, Click here

Leave a Reply

%d bloggers like this: