Take and Sum operator in LINQ using VB.NET
This article defines the basic use of the Take and Sum operators in LINQ.
Take Operator
Take operator will return first N number of element a sequence and then skips the remainder of the sequence.
For example
This example returns the first 7 element from the array of strings and then skips the remainder of the sequence.
Module Module1
Sub Main()
Dim names As String() = {"Micalgray", "Hari", "Tayler", "Bharat", "Monu", "Rohatash", "Naresh", "omveer", "Deepak", "smith"}
Dim query As IEnumerable(Of String) = names.Take(7)
For Each s As String In query
Console.WriteLine(s)
Next
End Sub
End Module
OUTPUT
Sum Operator
Sum operator returns sum of the numbers in a sequence.
For example
The following returns the total length of each of the strings in the names array.
Module Module1
Sub Main()
Dim names As String() = {"Micalgray", "Hari", "Tayler", "Bharat", "Monu", "Rohatash", "Naresh", "omveer", "Deepak", "smith"}
Dim TotalLength As Integer = names.Sum(Function(s) s.Length)
Console.WriteLine("The total length :" & TotalLength)
End Sub
End Module
OUTPUT