diff --git a/snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs b/snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs index b1acf2a1430..1b141106229 100644 --- a/snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs +++ b/snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs @@ -3357,5 +3357,44 @@ Sara Jones (Sara.jones@example.com) // } #endregion + + #region CountBy + static class CountBy + { + + public static void CountByDepartmentExample() + { + // + (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 + */ + // + } + + } + #endregion } } diff --git a/xml/System.Linq/Enumerable.xml b/xml/System.Linq/Enumerable.xml index 95791bba536..6062de43daa 100644 --- a/xml/System.Linq/Enumerable.xml +++ b/xml/System.Linq/Enumerable.xml @@ -3004,7 +3004,17 @@ Each chunk except the last one will be of size `size`. The last chunk will conta An to compare keys with. Returns the count of elements in the source sequence grouped by key. An enumerable containing the frequencies of each key occurrence in . - To be added. + + +