다음을 통해 공유


수량자 작업

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

다음 그림에서는 두 개의 다른 소스 시퀀스에 있는 두 개의 다른 수량자 작업을 설명합니다. 첫 번째 작업에서는 요소 중 하나 이상이 문자 'A'인지 묻고 결과는 true입니다. 두 번째 작업에서는 모든 요소가 문자 'A'인지 묻고 결과는 true입니다.

LINQ 수량자 작업

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

메서드

메서드 이름

설명

C# 쿼리 식 구문

Visual Basic 쿼리 식 구문

추가 정보

모두

시퀀스의 모든 요소가 특정 조건에 맞는지 확인합니다.

해당 사항 없음.

Aggregate … In … Into All(…)

Enumerable.All``1

Queryable.All``1

임의

시퀀스의 모든 요소가 특정 조건에 맞는지 확인합니다.

해당 사항 없음.

Aggregate … In … Into Any()

Enumerable.Any``1

Queryable.Any``1

포함

시퀀스에 지정된 요소가 들어 있는지 확인합니다.

해당 사항 없음.

해당 사항 없음.

Enumerable.Contains``1

Queryable.Contains``1

쿼리 식 구문 예제

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

다음 예제에서는 Aggregate 절 및 All``1 확장 메서드를 사용하여 가지고 있는 애완 동물 모두의 연령이 지정한 연령보다 많은 사람을 컬렉션에서 반환합니다.

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``1 확장 메서드를 사용하여 가지고 있는 애완 동물 중 하나 이상의 연령이 지정한 연령보다 많은 사람을 컬렉션에서 반환합니다.

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

참고 항목

작업

방법: 런타임에 동적으로 조건자 필터 지정(C# 프로그래밍 가이드)

방법: 지정된 단어 집합이 들어 있는 문장 쿼리(LINQ)

참조

Aggregate 절(Visual Basic)

System.Linq

개념

표준 쿼리 연산자 개요