LINQ SequenceEqual Method

In LINQ, the SequenceEqual method is useful to compare two collection sequences that are equal or not. It determines two sequences equal or not by comparing elements in a pair-wise manner, and two sequences contain an equal number of elements or not.

 

The LINQ sequenceequal method will return the Boolean value true if two sequence elements are equal and all the elements match in both the sequences; otherwise, it will throw false. 

Syntax of LINQ SequenceEqual Method

Following is the syntax of using the LINQ SequenceEqual method to check given two collections are equal or not

 

C# Code

 

var res1 = arr1.SequenceEqual(arr2);

VB.NET Code

 

Dim res1 = arr1.SequenceEqual(arr2)

If you observe the above syntax, we are using the LINQ SequenceEqual method to check whether “arr1” and “arr2” are equal or not.

Example of LINQ SequenceEqual Method

Following is the example of the LINQ SequenceEqual method to check the equality of two sequences.

 

C# Code

 

using System;
using System.Linq;

namespace Linqtutorials
{
  class Program
  {
    static void Main(string[] args)
    {
      string[] arr1 = { "welcome", "to", "tutlane", "com" };
      string[] arr2 = { "welcome", "TO", "TUTLANE", "com" };
      string[] arr3 = { "welcome", "to", "tutlane" };
      string[] arr4 = { "WELCOME", "TO", "TUTLANE" };
      var res1 = arr1.SequenceEqual(arr2);
      var res2 = arr1.SequenceEqual(arr2, StringComparer.OrdinalIgnoreCase);
      var res3 = arr1.SequenceEqual(arr3);
      var res4 = arr3.SequenceEqual(arr4, StringComparer.OrdinalIgnoreCase);
      Console.WriteLine("Result1: {0}", res1);
      Console.WriteLine("Result2: {0}", res2);
      Console.WriteLine("Result3: {0}", res3);
      Console.WriteLine("Result4: {0}", res4);
      Console.ReadLine();
    }
  }
}

VB.NET Code

 

Module Module1

Sub Main()
Dim arr1 As String() = {"welcome", "to", "tutlane", "com"}
Dim arr2 As String() = {"welcome", "TO", "TUTLANE", "com"}
Dim arr3 As String() = {"welcome", "to", "tutlane"}
Dim arr4 As String() = {"WELCOME", "TO", "TUTLANE"}
Dim res1 = arr1.SequenceEqual(arr2)
Dim res2 = arr1.SequenceEqual(arr2, StringComparer.OrdinalIgnoreCase)
Dim res3 = arr1.SequenceEqual(arr3)
Dim res4 = arr3.SequenceEqual(arr4, StringComparer.OrdinalIgnoreCase)
Console.WriteLine("Result1: {0}", res1)
Console.WriteLine("Result2: {0}", res2)
Console.WriteLine("Result3: {0}", res3)
Console.WriteLine("Result4: {0}", res4)
Console.ReadLine()
End Sub
End Module

If you observe the above example, we are comparing two collections using the SequenceEqual method to check if both sequences are equal or not.

 

We used another property, “OrdinalIgnoreCase” along with the SequenceEqual method to overcome the case sensitivity problem. The SequenceEqual method treats “TUTLANE” and “tutlane” as two different elements because of case sensitiveness.  

Result of LINQ SequenceEqual Method Example

Following is the result of the LINQ SequnceEqual method.

 

Result1: False
Result2: True
Result3: False
Result4: True

This is how we can use the LINQ SequenceEqual method to check the equality of two sequences.