Enumerable.CountBy<TSource,TKey> 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
키별로 그룹화된 소스 시퀀스의 요소 수를 반환합니다.
public static System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,int>> CountBy<TSource,TKey>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IEqualityComparer<TKey>? keyComparer = default);
static member CountBy : seq<'Source> * Func<'Source, 'Key> * System.Collections.Generic.IEqualityComparer<'Key> -> seq<System.Collections.Generic.KeyValuePair<'Key, int>>
<Extension()>
Public Function CountBy(Of TSource, TKey) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), Optional keyComparer As IEqualityComparer(Of TKey) = Nothing) As IEnumerable(Of KeyValuePair(Of TKey, Integer))
형식 매개 변수
- TSource
의 요소 형식입니다 source.
- TKey
keySelector반환된 키의 형식입니다.
매개 변수
- source
- IEnumerable<TSource>
계산할 요소가 들어 있는 시퀀스입니다.
- keySelector
- Func<TSource,TKey>
각 요소에 대한 키를 추출하는 함수입니다.
- keyComparer
- IEqualityComparer<TKey>
IEqualityComparer<T> 키를 비교할 수 있는 항목입니다.
반품
에서 각 키 source발생 빈도를 포함하는 열거형입니다.
예제
다음 코드 예제에서는 각 부서의 직원 수를 계산하는 데 사용하는 CountBy 방법을 보여 줍니다.
(string Name, int Age, string Department)[] employees =
{
("Saly", 23, "IT"),
("David", 25, "Sales"),
("Mahmoud", 22, "IT"),
("Qamar", 22, "HR"),
("Sara", 25, "IT"),
("John", 26, "HR"),
("Jaffar", 32, "Sales")
};
// Count the number of employees per department
var countPerDepartment = employees.CountBy(employee => employee.Department);
foreach (var item in countPerDepartment)
{
Console.WriteLine($"Department: {item.Key} - Employees Count: {item.Value}");
}
/*
This code produces the following output:
Department: IT - Employees Count: 3
Department: Sales - Employees Count: 2
Department: HR - Employees Count: 2
*/