Friday, 15 June 2012

.Net Tips : Querying the array using LINQ and Predicate in VB.Net Examples

You can also query the array variables using LINQ.
Here are sample example for this.
In this example there is an array of integer values which contains various number, Now we want to get numbers in array which is greater then 5.
We can do this without FOR loop.

There are two possible way to do this. One is using LINQ Query and Second is Using Predicate.

VB.net Example using LINQ Query :
Dim arrayNumbers As Integer() = {1, 2, 5, 9, 10, 1, 0, 9, 5, 6, 4, 3, 2}
Dim selectedNumber As Integer() = (From an In arrayNumbers Where an > 5 Select an).ToArray()

VB.net Example using Predicate :
Dim arrayNumbers As Integer() = {1, 2, 5, 9, 10, 1, 0, 9, 5, 6, 4, 3, 2}
Dim selectedNumberLambda As Integer() = arrayNumbers.Where(Function(o) o > 5).ToArray()

No comments:

Post a Comment