Wednesday 22 May 2013

.Net Tips : Check sequence of elements in two arrays are equal or not using LINQ With C#.Net and VB.Net Example

Using LINQ we can check sequence match on all elements in same order for two different array.
We are using "SequenceEqual" LINQ method to check. This method returns "true" if sequence match else return "false".

Here is LINQ "SequenceEqual" Method example.

In this example we take two string array one is "arrayOne" and second is "arrayTwo". Now we use "SequenceEqual" method to check that is both array has the same sequence of element. You can see output below.

C#. Net Example :
        var arrayOne = new string[] { "1", "vb.Net", "asp.Net" };
        var arrayTwo = new string[] { "1", "vb.Net", "asp.Net" };

        bool isMatch = arrayOne.SequenceEqual(arrayTwo);

        Response.Write("The sequences match :" + isMatch);

VB.Net Examples :
        Dim arrayOne = New String() {"1", "vb.Net", "asp.Net"}
        Dim arrayTwo = New String() {"1", "vb.Net", "asp.Net"}

        Dim isMatch As Boolean = arrayOne.SequenceEqual(arrayTwo)

        Response.Write("The sequences match :" & isMatch)

Output :

LINQ SequenceEqual Method Example

No comments:

Post a Comment