Medium csharp

Group employees by department

Problem

Group sample employees by department and print counts.

Hints
  • employees.GroupBy(e => e.Dept)

Your practice code

Ready — edit the code above and click Run.

Solution

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        var employees = new[] {
            new { Name = "A", Dept = "IT" },
            new { Name = "B", Dept = "HR" },
            new { Name = "C", Dept = "IT" }
        };
        var groups = employees.GroupBy(e => e.Dept);
        foreach (var g in groups)
            Console.WriteLine($"{g.Key}: {g.Count()}");
    }
}

Try solving on your own first, then reveal the official answer.

Explanation

GroupBy mirrors SQL GROUP BY—essential for enterprise .NET roles.

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details