다음을 통해 공유


방법: LINQ를 사용하여 ArrayList 쿼리(Visual Basic)

LINQ를 사용하여 ArrayList 등의 제네릭이 아닌 IEnumerable 컬렉션을 쿼리하는 경우 컬렉션에 있는 개체의 특정 형식을 반영하도록 범위 변수의 형식을 명시적으로 선언해야 합니다. 예를 들어 Student 개체의 ArrayList가 있는 경우 From 절은 다음과 같아야 합니다.

Dim query = From student As Student In arrList
'...

범위 변수의 형식을 지정하여 ArrayList의 각 항목을 Student로 캐스팅합니다.

명시적 형식 범위 변수를 쿼리 식에 사용하는 것은 Cast 메서드 호출과 같습니다. 지정된 캐스트를 수행할 수 없는 경우 Cast에서 예외를 throw합니다. CastOfType은 제네릭이 아닌 IEnumerable 형식에서 작동하는 두 가지 표준 쿼리 연산자 메서드입니다. Visual Basic에서는 데이터 원본에서 Cast 메서드를 명시적으로 호출하여 특정 범위 변수 형식을 확인해야 합니다. 자세한 내용은 쿼리 작업의 형식 관계(Visual Basic)를 참조하세요.

예시

다음 예제에서는 ArrayList에 대한 단순 쿼리를 보여 줍니다. 이 예제에서는 코드가 Add 메서드를 호출할 때 개체 이니셜라이저를 사용하지만 요구 사항은 아닙니다.

Imports System.Collections
Imports System.Linq

Module Module1

    Public Class Student
        Public Property FirstName As String
        Public Property LastName As String
        Public Property Scores As Integer()
    End Class

    Sub Main()

        Dim student1 As New Student With {.FirstName = "Svetlana",
                                     .LastName = "Omelchenko",
                                     .Scores = New Integer() {98, 92, 81, 60}}
        Dim student2 As New Student With {.FirstName = "Claire",
                                    .LastName = "O'Donnell",
                                    .Scores = New Integer() {75, 84, 91, 39}}
        Dim student3 As New Student With {.FirstName = "Cesar",
                                    .LastName = "Garcia",
                                    .Scores = New Integer() {97, 89, 85, 82}}
        Dim student4 As New Student With {.FirstName = "Sven",
                                    .LastName = "Mortensen",
                                    .Scores = New Integer() {88, 94, 65, 91}}

        Dim arrList As New ArrayList()
        arrList.Add(student1)
        arrList.Add(student2)
        arrList.Add(student3)
        arrList.Add(student4)

        ' Use an explicit type for non-generic collections
        Dim query = From student As Student In arrList
                    Where student.Scores(0) > 95
                    Select student

        For Each student As Student In query
            Console.WriteLine(student.LastName & ": " & student.Scores(0))
        Next
        ' Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.")
        Console.ReadKey()
    End Sub

End Module
' Output:
'   Omelchenko: 98
'   Garcia: 97

참고 항목