本文整理匯總了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