Enumerable.OfType<TResult>(IEnumerable) Metoda

Definicja

Filtruje elementy IEnumerable elementu na podstawie określonego typu.

public:
generic <typename TResult>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TResult> ^ OfType(System::Collections::IEnumerable ^ source);
public static System.Collections.Generic.IEnumerable<TResult> OfType<TResult> (this System.Collections.IEnumerable source);
static member OfType : System.Collections.IEnumerable -> seq<'Result>
<Extension()>
Public Function OfType(Of TResult) (source As IEnumerable) As IEnumerable(Of TResult)

Parametry typu

TResult

Typ do filtrowania elementów sekwencji.

Parametry

source
IEnumerable

Których elementy do filtrowania IEnumerable .

Zwraca

IEnumerable<TResult>

Element IEnumerable<T> zawierający elementy z sekwencji wejściowej typu TResult.

Wyjątki

source to null.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać OfType metody do filtrowania elementów elementu IEnumerable.

System.Collections.ArrayList fruits = new System.Collections.ArrayList(4);
fruits.Add("Mango");
fruits.Add("Orange");
fruits.Add("Apple");
fruits.Add(3.0);
fruits.Add("Banana");

// Apply OfType() to the ArrayList.
IEnumerable<string> query1 = fruits.OfType<string>();

Console.WriteLine("Elements of type 'string' are:");
foreach (string fruit in query1)
{
    Console.WriteLine(fruit);
}

// The following query shows that the standard query operators such as
// Where() can be applied to the ArrayList type after calling OfType().
IEnumerable<string> query2 =
    fruits.OfType<string>().Where(fruit => fruit.ToLower().Contains("n"));

Console.WriteLine("\nThe following strings contain 'n':");
foreach (string fruit in query2)
{
    Console.WriteLine(fruit);
}

// This code produces the following output:
//
// Elements of type 'string' are:
// Mango
// Orange
// Apple
// Banana
//
// The following strings contain 'n':
// Mango
// Orange
// Banana
' 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())

' This code produces the following output:
'
' Elements of type 'string' are:
' Mango
' Orange
' Apple
' Banana
'
' The following strings contain 'n':
' Mango
' Orange
' Banana

Uwagi

Ta metoda jest implementowana za pomocą odroczonego wykonania. Bezpośrednio zwracana wartość jest obiektem, który przechowuje wszystkie informacje wymagane do wykonania akcji. Zapytanie reprezentowane przez tę metodę nie jest wykonywane, dopóki obiekt nie zostanie wyliczone przez wywołanie metody GetEnumerator bezpośrednio lub przy użyciu foreach języka C# lub For Each w Visual Basic.

Metoda OfType<TResult>(IEnumerable) zwraca tylko te elementy, w source których można rzutować typ TResult. Aby zamiast tego otrzymać wyjątek, jeśli nie można rzutować elementu do typu TResult, użyj polecenia Cast<TResult>(IEnumerable).

Ta metoda jest jedną z niewielu standardowych metod operatorów zapytań, które można zastosować do kolekcji, która ma typ niesparametryzowany, taki jak ArrayList. Dzieje się tak, ponieważ OfType rozszerza typ IEnumerable. OfType nie można stosować tylko do kolekcji opartych na typie sparametryzowanym, ale kolekcje oparte również na typie niesparametryzowanym IEnumerable<T>IEnumerable .

OfType Stosując do kolekcji, która implementuje IEnumerablemetodę , uzyskujesz możliwość wykonywania zapytań względem kolekcji przy użyciu standardowych operatorów zapytań. Na przykład określenie argumentu Object typu , który ma zwracać OfType obiekt typu IEnumerable<Object> w języku C# lub IEnumerable(Of Object) Visual Basic, do którego można zastosować standardowe operatory zapytań.

Dotyczy