수량자 작업
업데이트: 2007년 11월
수량자 작업은 시퀀스의 요소 중 일부나 전체가 특정 조건을 만족하는지 여부를 나타내는 Boolean 값을 반환합니다.
다음 그림에서는 두 개의 다른 소스 시퀀스에 있는 두 개의 다른 수량자 작업을 설명합니다. 첫 번째 작업에서는 요소 중 하나 이상이 문자 'A'인지 묻고 결과는 true입니다. 두 번째 작업에서는 모든 요소가 문자 'A'인지 묻고 결과는 true입니다.
수량자 작업을 수행하는 표준 쿼리 연산자 메서드는 다음 단원에 나열되어 있습니다.
메서드
메서드 이름 |
설명 |
C# 쿼리 식 구문 |
Visual Basic 쿼리 식 구문 |
추가 정보 |
---|---|---|---|---|
All |
시퀀스의 모든 요소가 특정 조건에 맞는지 확인합니다. |
적용할 수 없음. |
Aggregate … In … Into All(…) |
|
Any |
시퀀스의 모든 요소가 특정 조건에 맞는지 확인합니다. |
적용할 수 없음. |
Aggregate … In … Into Any() |
|
Contains |
시퀀스에 지정된 요소가 들어 있는지 확인합니다. |
적용할 수 없음. |
적용할 수 없음. |
쿼리 식 구문 예제
이러한 예제에서는 Visual Basic의 Aggregate 절을 LINQ 쿼리에서 필터링 조건의 일부로 사용합니다.
다음 예제에서는 Aggregate 절 및 All<TSource> 확장 메서드를 사용하여 가지고 있는 애완 동물 모두의 연령이 지정한 연령보다 많은 사람을 컬렉션에서 반환합니다.
Class Person
Private _name As String
Private _pets As Pet()
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Pets() As Pet()
Get
Return _pets
End Get
Set(ByVal value As Pet())
_pets = value
End Set
End Property
End Class
Class Pet
Private _name As String
Private _age As Integer
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Age() As Integer
Get
Return _age
End Get
Set(ByVal value As Integer)
_age = value
End Set
End Property
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
Private _name As String
Private _pets As Pet()
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Pets() As Pet()
Get
Return _pets
End Get
Set(ByVal value As Pet())
_pets = value
End Set
End Property
End Class
Class Pet
Private _name As String
Private _age As Integer
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Age() As Integer
Get
Return _age
End Get
Set(ByVal value As Integer)
_age = value
End Set
End Property
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# 프로그래밍 가이드)