LINQ SequenceEqual Method

  Total Views : 148
  Comments : 0
 
  Keywords : Linq sequenceequal operation in c# / vb.net with example, Linq sequenceequal operator to compare two collection sequence in c# / vb.net with example, Linq enumerable.sequenceequal method

Here we will learn LINQ SequenceEqual method in c#, vb.net with example and how to use LINQ SequenceEqual method to compare two list sequences in c#, vb.net with example.

LINQ SequenceEqual Method

In LINQ sequenceequal method is used to compare two collection sequences are equal or not.  It determines two sequences equal or not by comparing elements in a pair-wise manner and two sequences contains equality number of elements or not.

 

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

Syntax of LINQ SequenceEqual Method

Following is the syntax of using 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 above syntax we are using LINQ SequenceEqual method to check whether “arr1” and “arr2” are equal or not.

Example of LINQ SequenceEqual Method

Following is the example of LINQ SequenceEqual method to check 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 above example we are comparing two collections using SequenceEqual method to check if both sequences are equal or not.

 

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

Output of LINQ SequenceEqual Method Example

Following is the result of LINQ SequnceEqual method.

 

Result1: False

Result2: True

Result3: False

Result4: True

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

 
 
 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User