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

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

語法Syntax

Where condition  

組件Parts

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.

備註Remarks

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子句必須評估為Boolean或相當的Boolean,例如評估為整數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子句使用邏輯運算子,例如AndOrAndAlsoOrElseIs,和IsNotYou can combine multiple expressions in a Where clause by using logical operators such as And, Or, AndAlso, OrElse, Is, and IsNot.

根據預設,查詢運算式,才會評估存取 — 比方說,當它們是資料繫結或透過在反覆執行For迴圈。By 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子句,確保適當的值用於Where子句在執行查詢的時間。If 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.

範例Example

下列查詢的運算式用法From子句來宣告範圍變數cust每個Customer物件中customers集合。The 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
    Console.WriteLine(cust.CompanyName)
  Next
End Sub

範例Example

下列範例會使用AndOr中的邏輯運算子Where子句。The 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)
    Next

    ' 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