.NET 9: Introducing LINQ Enhancements

Streamlining Data Workflows with LINQ Enhancements

Sukhpinder Singh | C# .Net
.Net Programming
Published in
3 min readMar 9, 2024

--

Photo by Nick Fewings on Unsplash

In the latest release of .NET 9, several enhancements have been introduced to LINQ, providing developers with more powerful and efficient ways to work with data. Among these enhancements are two new methods: CountBy and AggregateBy, along with the introduction of the Index<TSource> method. Let’s explore each of these additions in detail.

CountBy Method:

The CountBy method offers a convenient way to calculate the frequency of each key in a collection without the need for intermediate groupings via GroupBy. This method is particularly useful when working with data where key frequencies need to be determined efficiently.

Consider the following example, where we aim to find the most frequently occurring word in a given text string:

string sourceText = @"
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed non risus. Suspendisse lectus tortor, dignissim sit amet,
adipiscing nec, ultricies sed, dolor. Cras elementum ultrices amet diam.
";

// Find the most frequent word in the text.
KeyValuePair<string, int> mostFrequentWord = sourceText
.Split(new char[] { ' ', '.', ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(word => word.ToLowerInvariant())…

--

--

Sukhpinder Singh | C# .Net
.Net Programming

.Net Developer - Editor at .Net Programming with ~48K views per month, Looking for C# writers. Link: https://medium.com/c-sharp-programming