Posted in:

The seventeenth video in my exploring MoreLINQ series looks at the CountBy extension method. CountBy enables you to categorize each item in a sequence and keep track of how many items in each category have been seen. Of course, the same result could be achieved with LINQ's built-in GroupBy method, and counting the size of the groups, but CountBy is a much more memory efficient mechanism as it doesn't require the whole sequence to be stored in memory.

CountBy is also the ideal method to solve the "Counting Pets" problem in my Lunchtime LINQ challenge #2.

You can find all videos in this series here.

Want to learn more about LINQ? Be sure to check out my Pluralsight course LINQ Best Practices.