Take While 子句 (Visual Basic)

只要指定的條件為 true,即包含集合中的項目,並略過其餘項目。

語法

Take While expression  

組件

詞彙 定義
expression 必要。 表示要針對其測試元素之條件的運算式。 運算式必須傳回 Boolean 值或功能對應項,例如要評估為 BooleanInteger

備註

Take While 子句會從查詢結果的開頭包含元素,直到提供的 expression 傳回 false 為止。 expression 傳回 false 之後,查詢將會略過所有剩餘的元素。 會針對其餘結果忽略 expression

Take While 子句與 Where 子句不同,因為 Where 子句可用於從查詢中包含所有符合特定條件的元素。 Take While 子句只會在第一次不符合條件時包含元素。 使用已排序的查詢結果時,Take While 子句最實用。

範例

下列程式碼範例會使用 Take While 子句擷取結果,直到找不到任何訂單的第一個客戶為止。

Public Sub TakeWhileSample()
    Dim customers = GetCustomerList()

    ' Return customers until the first customer with no orders is found.
    Dim customersWithOrders = From cust In customers
                              Order By cust.Orders.Count Descending
                              Take While HasOrders(cust)

    For Each cust In customersWithOrders
        Console.WriteLine(cust.CompanyName & " (" & cust.Orders.Length & ")")
    Next
End Sub

Public Function HasOrders(ByVal cust As Customer) As Boolean
    If cust.Orders.Length > 0 Then Return True

    Return False
End Function

另請參閱