Skip While 子句 (Visual Basic)

只要指定的條件為 true,即略過集合中的項目,然後傳回其餘項目。

語法

Skip While expression  

組件

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

備註

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

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

您可以使用 Skip 子句,從查詢結果的開頭略過特定數目的結果。

範例

下列程式碼範例使用 Skip While 子句略過結果,直到找到來自美國的第一個客戶為止。

Public Sub SkipWhileSample()
    Dim customers = GetCustomerList()

    ' Return customers starting from the first U.S. customer encountered.
    Dim customerList = From cust In customers
                       Order By cust.Country
                       Skip While IsInternationalCustomer(cust)

    For Each cust In customerList
        Console.WriteLine(cust.CompanyName & vbTab & cust.Country)
    Next
End Sub

Public Function IsInternationalCustomer(ByVal cust As Customer) As Boolean
    If cust.Country = "USA" Then Return False

    Return True
End Function

另請參閱