Queryable.Cast<TResult>(IQueryable) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Converte gli elementi di un IQueryable oggetto nel tipo specificato.
public:
generic <typename TResult>
[System::Runtime::CompilerServices::Extension]
static System::Linq::IQueryable<TResult> ^ Cast(System::Linq::IQueryable ^ source);
public static System.Linq.IQueryable<TResult> Cast<TResult>(this System.Linq.IQueryable source);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static System.Linq.IQueryable<TResult> Cast<TResult>(this System.Linq.IQueryable source);
static member Cast : System.Linq.IQueryable -> System.Linq.IQueryable<'Result>
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
static member Cast : System.Linq.IQueryable -> System.Linq.IQueryable<'Result>
<Extension()>
Public Function Cast(Of TResult) (source As IQueryable) As IQueryable(Of TResult)
Parametri di tipo
- TResult
Tipo in cui convertire gli elementi di source .
Parametri
- source
- IQueryable
Oggetto IQueryable contenente gli elementi da convertire.
Restituisce
Oggetto IQueryable<T> contenente ogni elemento della sequenza di origine convertito nel tipo specificato.
- Attributi
Eccezioni
source è null.
Impossibile eseguire il cast di un elemento nella sequenza al tipo TResult.
Esempio
Nell'esempio di codice seguente viene illustrato come usare Cast<TResult>(IQueryable) per convertire gli oggetti in una sequenza in un tipo String.
// Create a list of objects.
List<object> words =
new List<object> { "green", "blue", "violet" };
// Cast the objects in the list to type 'string'
// and project the first letter of each string.
IEnumerable<string> query =
words.AsQueryable()
.Cast<string>()
.Select(str => str.Substring(0, 1));
foreach (string s in query)
Console.WriteLine(s);
/* This code produces the following output:
g
b
v
*/
' Create a list of objects.
Dim words As New List(Of Object)(New Object() {"green", "blue", "violet"})
' Cast the objects in the list to type 'string'
' and project the first letter of each string.
Dim query As IEnumerable(Of String) = _
words.AsQueryable() _
.Cast(Of String)() _
.Select(Function(str) str.Substring(0, 1))
For Each s As String In query
MsgBox(s)
Next
' This code produces the following output:
'
' g
' b
' v
Commenti
Il Cast<TResult>(IQueryable) metodo genera un oggetto MethodCallExpression che rappresenta la chiamata Cast<TResult>(IQueryable) stessa come metodo generico costruito. Passa quindi l'oggetto MethodCallExpression al CreateQuery(Expression) metodo dell'oggetto IQueryProvider rappresentato dalla Provider proprietà del source parametro .
Il comportamento della query che si verifica come risultato dell'esecuzione di un albero delle espressioni che rappresenta la chiamata Cast<TResult>(IQueryable) dipende dall'implementazione del tipo del source parametro. Il comportamento previsto è che converte i valori in nel source tipo TResult.