
Query Expression Syntax Examples
Skip
The following code example uses the Skip clause in Visual Basic to skip over the first four strings in an array of strings before returning the remaining strings in the array.
Dim words() As String = New String() {"an", "apple", "a", "day", "keeps", "the", "doctor", "away"}
Dim query = From word In words _
Skip 4
Dim sb As New System.Text.StringBuilder()
For Each str As String In query
sb.AppendLine(str)
Next
' Display the results.
MsgBox(sb.ToString())
' This code produces the following output:
' keeps
' the
' doctor
' away
SkipWhile
The following code example uses the Skip While clause in Visual Basic to skip over the strings in an array while the first letter of the string is "a". The remaining strings in the array are returned.
Dim words() As String = New String() {"an", "apple", "a", "day", "keeps", "the", "doctor", "away"}
Dim query = From word In words _
Skip While word.Substring(0, 1) = "a"
Dim sb As New System.Text.StringBuilder()
For Each str As String In query
sb.AppendLine(str)
Next
' Display the results.
MsgBox(sb.ToString())
' This code produces the following output:
' day
' keeps
' the
' doctor
' away
Take
The following code example uses the Take clause in Visual Basic to return the first two strings in an array of strings.
Dim words() As String = New String() {"an", "apple", "a", "day", "keeps", "the", "doctor", "away"}
Dim query = From word In words _
Take 2
Dim sb As New System.Text.StringBuilder()
For Each str As String In query
sb.AppendLine(str)
Next
' Display the results.
MsgBox(sb.ToString())
' This code produces the following output:
' an
' apple
TakeWhile
The following code example uses the Take While clause in Visual Basic to return strings from an array while the length of the string is five or less.
Dim words() As String = New String() {"an", "apple", "a", "day", "keeps", "the", "doctor", "away"}
Dim query = From word In words _
Take While word.Length < 6
Dim sb As New System.Text.StringBuilder()
For Each str As String In query
sb.AppendLine(str)
Next
' Display the results.
MsgBox(sb.ToString())
' This code produces the following output:
' an
' apple
' a
' day
' keeps
' the