Queryable.Cast<TResult>(IQueryable) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
IQueryableの要素を指定した型に変換します。
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)
型パラメーター
- TResult
sourceの要素を変換する型。
パラメーター
- source
- IQueryable
変換する要素を含む IQueryable 。
戻り値
指定した型に変換されたソース シーケンスの各要素を含む IQueryable<T> 。
- 属性
例外
source は nullです。
シーケンス内の要素を型 TResultにキャストすることはできません。
例
次のコード例では、 Cast<TResult>(IQueryable) を使用してシーケンス内のオブジェクトを型 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
注釈
Cast<TResult>(IQueryable) メソッドは、構築されたジェネリック メソッドとしての呼び出しCast<TResult>(IQueryable)自体を表すMethodCallExpressionを生成します。 次に、source パラメーターのProvider プロパティで表されるIQueryProviderのCreateQuery(Expression) メソッドにMethodCallExpressionを渡します。
呼び出し Cast<TResult>(IQueryable) を表す式ツリーを実行した結果として発生するクエリ動作は、 source パラメーターの型の実装によって異なります。 予想される動作は、 source の値を型 TResultに変換することです。