← Back

New CountBy LINQ Method

2026-01-02 16:11

✨ I remember the first time I needed to count items by category in C#. Of course, LINQ had me covered — but the code always felt… verbose.

var counts = items .GroupBy(x => x.Category) .Select(g => new );

It worked, but it never felt elegant.

Fast forward to .NET 9 and Microsoft gave us a little gift 🎁: CountBy

var counts = items.CountBy(x => x.Category);

That’s it. One line. Clean, expressive, and exactly what I meant. For me, this is more than just syntactic sugar — it’s about making code more readable and intuitive.

Because at the end of the day, clarity is what keeps teams productive.

👉 Curious to hear: how are you using CountBy (or planning to) in your projects?

I’m already using it for analytics dashboards and log summarization.

#dotnet #csharp #linq #net9 #developerexperience #cleanCode

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please reload the page.