Muokkaa

Jaa


Filtering Data (Visual Basic)

Filtering refers to the operation of restricting the result set to contain only those elements that satisfy a specified condition. It is also known as selection.

The following illustration shows the results of filtering a sequence of characters. The predicate for the filtering operation specifies that the character must be 'A'.

Diagram that shows a LINQ filtering operation

The standard query operator methods that perform selection are listed in the following section.

Methods

Method Name Description Visual Basic Query Expression Syntax More Information
OfType Selects values, depending on their ability to be cast to a specified type. Not applicable. Enumerable.OfType

Queryable.OfType
Where Selects values that are based on a predicate function. Where Enumerable.Where

Queryable.Where

Query Expression Syntax Example

The following example uses the Where to filter from an array those strings that have a specific length.

Dim words() As String = {"the", "quick", "brown", "fox", "jumps"}

Dim query = From word In words
            Where word.Length = 3
            Select word

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:

' the
' fox

See also