本文整理汇总了VB.NET中System.Linq.Enumerable.OfType<TResult>方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Enumerable.OfType<TResult>方法的具体用法?VB.NET Enumerable.OfType<TResult>怎么用?VB.NET Enumerable.OfType<TResult>使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了Enumerable.OfType<TResult>方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: OfType
' Create an ArrayList and add items to it.
Dim fruits As New System.Collections.ArrayList(4)
fruits.Add("Mango")
fruits.Add("Orange")
fruits.Add("Apple")
fruits.Add(3.0)
fruits.Add("Banana")
' Apply OfType(Of String)() to the ArrayList
' to filter out non-string items.
Dim query1 As IEnumerable(Of String) = fruits.OfType(Of String)()
' Print the results.
Dim output As New System.Text.StringBuilder("Elements of type 'string' are:" _
& vbCrLf)
For Each fruit As String In query1
output.AppendLine(fruit)
Next
' The following query shows that the standard query operators such as
' Where() can be applied to the ArrayList type after calling OfType().
Dim query2 As IEnumerable(Of String) =
fruits.OfType(Of String)().Where(Function(fruit) _
fruit.ToLower().Contains("n"))
output.AppendLine(vbCrLf & "The following strings contain 'n':")
For Each fruit As String In query2
output.AppendLine(fruit)
Next
' Display the output.
Console.WriteLine(output.ToString())
输出:
Elements of type 'string' are: Mango Orange Apple Banana The following strings contain 'n': Mango Orange Banana