Queryable.Cast<TResult>(IQueryable) 메서드

정의

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);
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<TResult>

지정된 형식으로 변환되는 소스 시퀀스의 각 요소가 들어 있는 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 합니다. 그런 다음 매개 변수의 IQueryProvider CreateQuery(Expression) 속성으로 표현되는 메서드에 Provider 전달 MethodCallExpression 합니다source.

호출 Cast<TResult>(IQueryable) 을 나타내는 식 트리를 실행한 결과로 발생하는 쿼리 동작은 매개 변수 형식 source 의 구현에 따라 달라집니다. 예상되는 동작은 값을 source 형식 TResult으로 변환하는 것입니다.

적용 대상