Queryable.Cast<TResult>(IQueryable) Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
öğesinin IQueryable öğelerini belirtilen türe dönüştürür.
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)
Tür Parametreleri
- TResult
öğelerinin source dönüştürüldüğü tür.
Parametreler
- source
- IQueryable
IQueryable Dönüştürülecek öğeleri içeren.
Döndürülenler
IQueryable<T> Belirtilen türe dönüştürülen kaynak dizisinin her öğesini içeren bir.
- Öznitelikler
Özel durumlar
source, null'e eşittir.
dizisindeki bir öğe türüne TResulttürlenemez.
Örnekler
Aşağıdaki kod örneği, bir dizideki nesneleri yazmak Stringüzere dönüştürmek için nasıl kullanılacağını Cast<TResult>(IQueryable) gösterir.
// 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
Açıklamalar
yöntemi, Cast<TResult>(IQueryable) kendisini oluşturulan genel bir MethodCallExpression yöntem olarak çağıran Cast<TResult>(IQueryable) bir oluşturur. Ardından parametresinin özelliği tarafından Provider temsil edilen yöntemine sourceIQueryProvider iletirMethodCallExpression.CreateQuery(Expression)
Çağrıyı Cast<TResult>(IQueryable) temsil eden bir ifade ağacının yürütülmesi sonucunda oluşan sorgu davranışı, parametre türünün uygulanmasına source bağlıdır. Beklenen davranış, içindeki source değerleri türüne TResultdönüştürmesidir.