Take While 句 (Visual Basic)

指定された条件が true である限り、コレクションの要素を含むようにし、残りの要素をバイパスします。

構文

Take While expression  

指定項目

用語 定義
expression 必須です。 次の要素をテストするための条件を表す式。 式は、Boolean 値または同等の機能 (Boolean として評価される Integer など) を返す必要があります。

Remarks

Take While 句には、クエリ結果の先頭から、指定された expressionfalse が返されるまでの要素が含まれます。 expressionfalse を返すと、クエリでは残りのすべての要素がバイパスされます。 残りの結果に対して、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

関連項目