Share via


데이터 형식 변환

변환 메서드는 입력 개체의 형식을 변경합니다.

LINQ 쿼리의 변환 연산자는 다양한 응용 프로그램에서 유용합니다. 다음은 이를 보여 주는 몇 가지 예입니다.

메서드

다음 표에서는 데이터 형식 변환을 수행하는 표준 쿼리 연산자 메서드를 보여 줍니다.

이 표에서 이름이 "As"로 시작하는 변환 메서드는 소스 컬렉션의 정적 형식을 변경하지만 이를 열거하지는 않습니다. 이름이 "To"로 시작하는 메서드는 소스 컬렉션을 열거하고 항목을 해당 컬렉션 형식에 놓습니다.

메서드 이름

설명

C# 쿼리 식 구문

Visual Basic 쿼리 식 구문

추가 정보

AsEnumerable

입력을 IEnumerable<T>로 형식화하여 반환합니다.

해당 사항 없음.

해당 사항 없음.

Enumerable.AsEnumerable<TSource>

AsQueryable

제네릭 IEnumerable을 제네릭 IQueryable로 변환합니다.

해당 사항 없음.

해당 사항 없음.

Queryable.AsQueryable

Cast

컬렉션의 요소를 지정한 형식으로 캐스팅합니다.

명시적으로 형식화된 범위 변수를 사용합니다. 예를 들면 다음과 같습니다.

from string str in words

From … As …

Enumerable.Cast<TResult>

Queryable.Cast<TResult>

OfType

지정된 형식으로 캐스팅할 기능에 따라 값을 필터링합니다.

해당 사항 없음.

해당 사항 없음.

Enumerable.OfType<TResult>

Queryable.OfType<TResult>

ToArray

컬렉션을 배열로 변환합니다. 이 메서드는 쿼리를 강제 실행합니다.

해당 사항 없음.

해당 사항 없음.

Enumerable.ToArray<TSource>

ToDictionary

키 선택기 함수에 따라 요소를 Dictionary<TKey, TValue>에 배치합니다. 이 메서드는 쿼리를 강제 실행합니다.

해당 사항 없음.

해당 사항 없음.

Enumerable.ToDictionary

ToList

컬렉션을 List<T>로 변환합니다. 이 메서드는 쿼리를 강제 실행합니다.

해당 사항 없음.

해당 사항 없음.

Enumerable.ToList<TSource>

ToLookup

키 선택기 함수에 따라 요소를 Lookup<TKey, TElement>(일대다 사전)에 배치합니다. 이 메서드는 쿼리를 강제 실행합니다.

해당 사항 없음.

해당 사항 없음.

Enumerable.ToLookup

쿼리 식 구문 예제

다음 코드 예제에서는 C#에서 명시적으로 형식화된 범위 변수나 Visual Basic의 From As 절을 사용하여 하위 형식에서만 사용 가능한 멤버에 액세스하기 전에 하위 형식에 형식을 캐스팅합니다.

Class Plant
    Public Property Name As String
End Class

Class CarnivorousPlant
    Inherits Plant
    Public Property TrapType As String
End Class

Sub Cast()

    Dim plants() As Plant = { 
        New CarnivorousPlant With {.Name = "Venus Fly Trap", .TrapType = "Snap Trap"}, 
        New CarnivorousPlant With {.Name = "Pitcher Plant", .TrapType = "Pitfall Trap"}, 
        New CarnivorousPlant With {.Name = "Sundew", .TrapType = "Flypaper Trap"}, 
        New CarnivorousPlant With {.Name = "Waterwheel Plant", .TrapType = "Snap Trap"}}

    Dim query = From plant As CarnivorousPlant In plants 
                Where plant.TrapType = "Snap Trap" 
                Select plant

    Dim sb As New System.Text.StringBuilder()
    For Each plant In query
        sb.AppendLine(plant.Name)
    Next

    ' Display the results.
    MsgBox(sb.ToString())

    ' This code produces the following output:

    ' Venus Fly Trap
    ' Waterwheel Plant

End Sub
class Plant
{
    public string Name { get; set; }
}

class CarnivorousPlant : Plant
{
    public string TrapType { get; set; }
}

static void Cast()
{
    Plant[] plants = new Plant[] {
        new CarnivorousPlant { Name = "Venus Fly Trap", TrapType = "Snap Trap" },
        new CarnivorousPlant { Name = "Pitcher Plant", TrapType = "Pitfall Trap" },
        new CarnivorousPlant { Name = "Sundew", TrapType = "Flypaper Trap" },
        new CarnivorousPlant { Name = "Waterwheel Plant", TrapType = "Snap Trap" }
    };

    var query = from CarnivorousPlant cPlant in plants
                where cPlant.TrapType == "Snap Trap"
                select cPlant;

    foreach (Plant plant in query)
        Console.WriteLine(plant.Name);

    /* This code produces the following output:

        Venus Fly Trap
        Waterwheel Plant
    */
}

참고 항목

작업

방법: LINQ를 사용하여 ArrayList 쿼리

참조

from 절(C# 참조)

From 절(Visual Basic)

System.Linq

개념

표준 쿼리 연산자 개요

LINQ 쿼리 식(C# 프로그래밍 가이드)