다음을 통해 공유


LINQ의 수량자 작업(C#)

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

중요합니다

이 샘플은 System.Collections.Generic.IEnumerable<T> 데이터 원본을 사용합니다. System.Linq.IQueryProvider 기반 데이터 원본은 System.Linq.IQueryable<T> 데이터 원본과 식 트리를 사용합니다. 식 트리에는 허용되는 C# 구문에 대한 제한 사항이 있습니다. 또한 IQueryProviderEF Core와 같이 더 많은 제한을 가할 수 있습니다. 데이터 원본에 대한 설명서를 확인합니다.

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

LINQ 수량자 작업

메서드 이름 설명 C# 쿼리 식 구문 더 많은 정보
모두 시퀀스의 모든 요소가 조건을 충족하는지 여부를 결정합니다. 적용할 수 없습니다. Enumerable.All
Queryable.All
어느 것이든 시퀀스의 요소가 조건을 충족하는지 여부를 결정합니다. 적용할 수 없습니다. Enumerable.Any
Queryable.Any
포함함 시퀀스에 지정된 요소가 포함되어 있는지 여부를 확인합니다. 적용할 수 없습니다. Enumerable.Contains
Queryable.Contains

모두

다음 예제에서는 모든 시험에서 All 70점 이상의 점수를 받은 학생을 찾습니다.

비고

표준 쿼리 연산자 개요 문서에서 이 영역에 대한 공통 데이터 원본을 참조할 수 있습니다.

IEnumerable<string> names = from student in students
                            where student.Scores.All(score => score > 70)
                            select $"{student.FirstName} {student.LastName}: {string.Join(", ", student.Scores.Select(s => s.ToString()))}";

foreach (string name in names)
{
    Console.WriteLine($"{name}");
}

// This code produces the following output:
//
// Cesar Garcia: 71, 86, 77, 97
// Nancy Engström: 75, 73, 78, 83
// Ifunanya Ugomma: 84, 82, 96, 80

어느 것이든

다음 예제에서는 이 방법을 사용하여 Any 모든 시험에서 95점보다 큰 점수를 받은 학생을 찾습니다.

IEnumerable<string> names = from student in students
                            where student.Scores.Any(score => score > 95)
                            select $"{student.FirstName} {student.LastName}: {student.Scores.Max()}";

foreach (string name in names)
{
    Console.WriteLine($"{name}");
}

// This code produces the following output:
//
// Svetlana Omelchenko: 97
// Cesar Garcia: 97
// Debra Garcia: 96
// Ifeanacho Jamuike: 98
// Ifunanya Ugomma: 96
// Michelle Caruana: 97
// Nwanneka Ifeoma: 98
// Martina Mattsson: 96
// Anastasiya Sazonova: 96
// Jesper Jakobsson: 98
// Max Lindgren: 96

포함함

다음 예제에서는 시험에서 Contains 정확히 95점을 받은 학생을 찾습니다.

IEnumerable<string> names = from student in students
                            where student.Scores.Contains(95)
                            select $"{student.FirstName} {student.LastName}: {string.Join(", ", student.Scores.Select(s => s.ToString()))}";

foreach (string name in names)
{
    Console.WriteLine($"{name}");
}

// This code produces the following output:
//
// Claire O'Donnell: 56, 78, 95, 95
// Donald Urquhart: 92, 90, 95, 57

참고하십시오