+92 332 4229 857 99ProjectIdeas@Gmail.com

LINQ All() Method (Vb.net)



The following code shows you how to check all elements in that array are equal to some specific number or are greater than it. For example array contains {2,1,6,5,7} if you check every element in this array is greater than 5 then you can see not every element is greater than 5 so the result of LINQ All() method returns false and if that condition meets then it returns true. See the code below.





Dim number() As Integer = {1, 3, 2, 5, 7}

Dim query2 = number.All(Function(x) x >= 5)

Console.WriteLine(query2)

Output of the program is:
False  (because not every element is greater than 5)

But, if you modified array to this, and see the output, Then:

Dim number() As Integer = {6, 8, 9, 5, 7}

Dim query2 = number.All(Function(x) x >= 5)

Console.WriteLine(query2)

Output of the program is:
True  (because every element is greater than 5)

Another Example,


' Create an array of Names
Dim allNames() As String = {"Osama", "Saad", "Bilal", "Maryam", "Shahbaz"}

' Determine whether all names in the array start with "B".
Dim query As BooleanallNames.All(Function(name) name.StartsWith("B"))

' Display the output.
Console.Write(query)
  
Output of the program is:
False  (because except Bilal there isnt any element which starts with "B")

0 comments: