Where 子句 (Visual Basic)Where Clause (Visual Basic)

指定查詢的篩選準則。Specifies the filtering condition for a query.


Where condition  


必要。Required. 運算式,判斷集合中目前專案的值是否包含在輸出集合中。An expression that determines whether the values for the current item in the collection are included in the output collection. 運算式必須評估為 Boolean 值或對等的 Boolean 值。The expression must evaluate to a Boolean value or the equivalent of a Boolean value. 如果條件評估為 True ,則專案會包含在查詢結果中,否則會從查詢結果中排除元素。If the condition evaluates to True, the element is included in the query result; otherwise, the element is excluded from the query result.


Where子句可讓您只選取符合特定準則的元素來篩選查詢資料。The Where clause enables you to filter query data by selecting only elements that meet certain criteria. 其值會讓 Where 子句評估為的元素 True 會包含在查詢結果中,而其他元素則會排除。Elements whose values cause the Where clause to evaluate to True are included in the query result; other elements are excluded. 子句中使用的運算式 Where 必須評估為或的對 BooleanBoolean ,例如在 False 其值為零時評估為的整數。The expression that is used in a Where clause must evaluate to a Boolean or the equivalent of a Boolean, such as an Integer that evaluates to False when its value is zero. 您可以 Where 使用邏輯運算子(例如 AndOrAndAlsoOrElseIsIsNot )結合子句中的多個運算式。You can combine multiple expressions in a Where clause by using logical operators such as And, Or, AndAlso, OrElse, Is, and IsNot.

根據預設,查詢運算式在存取之前不會進行評估,例如,當資料系結或在迴圈中反復執行時 ForBy default, query expressions are not evaluated until they are accessed—for example, when they are data-bound or iterated through in a For loop. 因此,在 Where 存取查詢之前,不會評估子句。As a result, the Where clause is not evaluated until the query is accessed. 如果您在子句中使用的查詢外部有值 Where ,請確定在執行查詢時,子句中使用了適當的值 WhereIf you have values external to the query that are used in the Where clause, ensure that the appropriate value is used in the Where clause at the time the query is executed. 如需查詢執行的詳細資訊,請參閱撰寫您的第一個 LINQ 查詢For more information about query execution, see Writing Your First LINQ Query.

您可以在子句內呼叫函式 Where ,以從集合中目前專案的值執行計算或運算。You can call functions within a Where clause to perform a calculation or operation on a value from the current element in the collection. 呼叫子句中的函式 Where 可能會導致查詢在定義時立即執行,而不是存取時。Calling a function in a Where clause can cause the query to be executed immediately when it is defined instead of when it is accessed. 如需查詢執行的詳細資訊,請參閱撰寫您的第一個 LINQ 查詢For more information about query execution, see Writing Your First LINQ Query.


下列查詢運算式會使用 From 子句, cust 針對集合中的每個物件宣告範圍變數 Customer customersThe following query expression uses a From clause to declare a range variable cust for each Customer object in the customers collection. Where子句會使用範圍變數,將輸出限制為來自指定區域的客戶。The Where clause uses the range variable to restrict the output to customers from the specified region. For Each迴圈會在查詢結果中顯示每個客戶的公司名稱。The For Each loop displays the company name for each customer in the query result.

Sub DisplayCustomersForRegion(ByVal customers As List(Of Customer),
                              ByVal region As String)

    Dim customersForRegion = From cust In customers
                             Where cust.Region = region

    For Each cust In customersForRegion
End Sub


下列範例會 And Or 在子句中使用和邏輯運算子 WhereThe following example uses And and Or logical operators in the Where clause.

Private Sub DisplayElements()
    Dim elements As List(Of Element) = BuildList()

    ' Get a list of elements that have an atomic number from 12 to 14,
    ' or that have a name that ends in "r".
    Dim subset = From theElement In elements
                 Where (theElement.AtomicNumber >= 12 And theElement.AtomicNumber < 15) _
                 Or theElement.Name.EndsWith("r")
                 Order By theElement.Name

    For Each theElement In subset
        Console.WriteLine(theElement.Name & " " & theElement.AtomicNumber)

    ' Output:
    '  Aluminum 13
    '  Magnesium 12
    '  Silicon 14
    '  Sulfur 16
End Sub

Private Function BuildList() As List(Of Element)
    Return New List(Of Element) From
            {New Element With {.Name = "Sodium", .AtomicNumber = 11}},
            {New Element With {.Name = "Magnesium", .AtomicNumber = 12}},
            {New Element With {.Name = "Aluminum", .AtomicNumber = 13}},
            {New Element With {.Name = "Silicon", .AtomicNumber = 14}},
            {New Element With {.Name = "Phosphorous", .AtomicNumber = 15}},
            {New Element With {.Name = "Sulfur", .AtomicNumber = 16}}
End Function

Public Class Element
    Public Property Name As String
    Public Property AtomicNumber As Integer
End Class

另請參閱See also