Friday, 31 May 2013

.Net Tips : LINQ SkipWhile Method Example With C#.Net and VB.Net

SkipWhile LINQ method to get the elements of the array starting from our given condition matched.

Here is LINQ "SkipWhile" Method example.
In this example we take one integer array "arrayNumbers" which contains integer values. Now we use "SkipWhile" method to get all elements starting from first element divisible by 3. In the lambda expression, "n" is the input parameter that identifies each element in the collection in succession. It is inferred to be of type int because numbers is an int array.

C#. Net Example :
        int[] arrayNumbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

        
        var objResult = arrayNumbers.SkipWhile(n => n % 3 != 0);

        Response.Write("<b>All elements starting from first element divisible by 3 : </b>");
        Response.Write("<br/>");
        foreach (var n in objResult)
        {
            Response.Write(n);
            Response.Write("<br/>");
        }

VB.Net Examples :
        Dim arrayNumbers As Integer() = {4, 5, 0, 3, 9, 8, 6, 7, 1, 2, 0}

        Dim objResult = arrayNumbers.SkipWhile(Function(n) n Mod 3 <> 0)

        Response.Write("<b>All elements starting from first element divisible by 3 : </b>")
        Response.Write("<br/>")

        For Each num As Integer In objResult
            Response.Write(num)
            Response.Write("<br/>")
        Next

Output :
LINQ SkipWhile Method Example

To view "TakeWhile" LINQ method example Click Here...


No comments:

Post a Comment

Post a Comment