Take While 子句 (Visual Basic)
包括集合中指定条件为 true
的任何元素,并绕过剩余元素。
语法
Take While expression
组成部分
术语 | 定义 |
---|---|
expression |
必需。 一个表达式,表示要测试其元素的条件。 表达式必须返回值 Boolean 或等效函数,例如要计算为 Boolean 的 Integer 。 |
注解
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
另请参阅
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
.NET feedback
The .NET documentation is open source. Provide feedback here.
反馈
提交和查看相关反馈