다음을 통해 공유


수량자 연산(Visual Basic)

수량자 작업은 시퀀스의 요소 중 일부 또는 전부가 조건을 충족하는지 여부를 나타내는 값을 반환 Boolean 합니다.

다음 그림에서는 두 개의 서로 다른 소스 시퀀스에 대한 두 개의 서로 다른 수량자 작업을 보여 줍니다. 첫 번째 작업은 요소 중 어느 것이 'A' 문자인지 묻습니다. 두 번째 작업은 모든 요소가 문자 'A'인지 묻습니다. 이 예제에서는 두 메서드가 모두 반환 true 됩니다.

LINQ 수량자 작업

수량자 작업을 수행하는 표준 쿼리 연산자 메서드는 다음 섹션에 나열되어 있습니다.

메서드

메서드 이름 설명 Visual Basic 쿼리 식 구문 더 많은 정보
모두 시퀀스의 모든 요소가 조건을 충족하는지 여부를 결정합니다. Aggregate … In … Into All(…) Enumerable.All

Queryable.All
어느 것이든 시퀀스의 요소가 조건을 충족하는지 여부를 결정합니다. Aggregate … In … Into Any() Enumerable.Any

Queryable.Any
포함함 시퀀스에 지정된 요소가 포함되어 있는지 여부를 확인합니다. 적용할 수 없습니다. Enumerable.Contains

Queryable.Contains

쿼리 식 구문 예제

다음 예제에서는 LINQ 쿼리에서 필터링 조건의 일부로 Visual Basic의 절을 사용합니다 Aggregate .

다음 예제에서는 Aggregate 절과 All 확장 메서드를 사용하여 애완동물이 모두 지정된 나이보다 많은 사람들을 컬렉션에서 반환합니다.

Class Person
    Public Property Name As String
    Public Property Pets As Pet()
End Class

Class Pet
    Public Property Name As String
    Public Property Age As Integer
End Class

Sub All()
    Dim barley As New Pet With {.Name = "Barley", .Age = 4}
    Dim boots As New Pet With {.Name = "Boots", .Age = 1}
    Dim whiskers As New Pet With {.Name = "Whiskers", .Age = 6}
    Dim bluemoon As New Pet With {.Name = "Blue Moon", .Age = 9}
    Dim daisy As New Pet With {.Name = "Daisy", .Age = 3}

    Dim charlotte As New Person With {.Name = "Charlotte", .Pets = New Pet() {barley, boots}}
    Dim arlene As New Person With {.Name = "Arlene", .Pets = New Pet() {whiskers}}
    Dim rui As New Person With {.Name = "Rui", .Pets = New Pet() {bluemoon, daisy}}

    ' Create the list of Person objects that will be queried.
    Dim people As New System.Collections.Generic.List(Of Person)(New Person() {charlotte, arlene, rui})

    Dim query = From pers In people
                Where (Aggregate pt In pers.Pets Into All(pt.Age > 2))
                Select pers.Name

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

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

    ' This code produces the following output:

    ' Arlene
    ' Rui

End Sub

다음 예제에서는 Aggregate 절과 Any 확장 메서드를 사용하여, 지정된 연령보다 오래된 애완동물을 하나 이상 가진 사람들을 컬렉션에서 반환합니다.

Class Person
    Public Property Name As String
    Public Property Pets As Pet()
End Class

Class Pet
    Public Property Name As String
    Public Property Age As Integer
End Class

Sub Any()
    Dim barley As New Pet With {.Name = "Barley", .Age = 4}
    Dim boots As New Pet With {.Name = "Boots", .Age = 1}
    Dim whiskers As New Pet With {.Name = "Whiskers", .Age = 6}
    Dim bluemoon As New Pet With {.Name = "Blue Moon", .Age = 9}
    Dim daisy As New Pet With {.Name = "Daisy", .Age = 3}

    Dim charlotte As New Person With {.Name = "Charlotte", .Pets = New Pet() {barley, boots}}
    Dim arlene As New Person With {.Name = "Arlene", .Pets = New Pet() {whiskers}}
    Dim rui As New Person With {.Name = "Rui", .Pets = New Pet() {bluemoon, daisy}}

    ' Create the list of Person objects that will be queried.
    Dim people As New System.Collections.Generic.List(Of Person)(New Person() {charlotte, arlene, rui})

    Dim query = From pers In people
                Where (Aggregate pt In pers.Pets Into Any(pt.Age > 7))
                Select pers.Name

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

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

    ' This code produces the following output:

    ' Rui

End Sub

참고하십시오