Friday, 31 May 2013

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...


Thursday, 23 May 2013

You can get element from array until your given condition is satisfied.
TakeWhile LINQ method to return elements starting from the beginning of the array until a number is read whose value satisfied according to our given criteria.


Here is LINQ "TakeWhile" Method example.

Wednesday, 22 May 2013

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.

Tuesday, 14 May 2013

There are situations were we have string array which contains many value and we want to check that particular word or string part exist in all values.
We want to avoid iteration of array at that time we can use LINQ's "Any" method. Using one line code we can check.

Here is example for this :
In this example we take on string array "ProductArray" and insert some values. Now we want to check that "er" word is contain any values of this array or not using "Contains" method. You can check output below.

C#. Net Example :
        string[] ProductArray = { "CPU", "Computer", "DVD", ".Net" };

        bool isExist = ProductArray.Any(o => o.Contains("er"));

        Response.Write("There is a word in the list that contains 'er': <b>" + isExist + "</b>");

VB.Net Examples :
        Dim ProductArray As String() = {"CPU", "Computer", "DVD", ".Net"}

        Dim isExist As Boolean = ProductArray.Any(Function(o) o.Contains("er"))

        Response.Write("There is a word in the list that contains 'er': <b>" & isExist & "</b>")

Output :

Wednesday, 8 May 2013

Using LINQ we can convert array in to dictionary object. We can use "ToDictionary" LINQ method to get dictionary object from array.
But we need to make sure that the key value should be unique in array, otherwise it will generate error "An item with the same key has already been added.".

Here is example for that.
In this example we take one two dimensional array "CustomerDetails". This array contains ID and Name of customer. Now using "ToDictionary" method of this array we can get dictionary object of type "Dictionary<int, string>". You can get your desirable type of Dictionary object.

Thursday, 2 May 2013

Using LINQ we can get specific type of object from Object Type array which contains many types of object. We are using "OfType" generic method to return only the elements of the array that  we specified.

Here is example for this.
In this example we take one array whose type is object and which contains many types of object, i.e. string, integer, double, Dictionary etc...
Now we get only doubles value using LINQ and display, after that, we get Dictionary object using LINQ and display. You can see the output for that.