LINQ, like everything else in.NET, evolves over time. The upcoming.NET 6 release will include a number of exciting new features, including a set of new LINQ capabilities.
Chunking is perhaps the most significant change to LINQ in.NET 6. If you’re anything like me, you’ve had to work with enormous collections of things previously and wished you could break them down into little pages or “chunks” to work with.
const int PAGE_SIZE = 5; IEnumerable<Movie> chunks = movies.Chunk(PAGE_SIZE);
Range Support for Take
A member of the.NET community modified the Take method to allow it to work with a Range argument, building on the previous notion of utilizing Indices to optimize code:
Movie middleMovies = movies.Take(6..10);
This method is beautifully simple, skipping until the sixth element before grabbing the next four items. Other range expressions, such as..3 or 5.., also work, allowing you to grab objects from collections based on certain indices.
MaxBy and MinBy
Finally,.NET 6 includes the MinBy and MaxBy LINQ extension methods for.NET developers. Based on a specific arrow function you supply, these two techniques allow you to browse through your collection and locate the greatest or smallest of anything.
Prior to.NET 6, To identify the entity that had the greatest or lowest of anything, you’d have to discover the value using Max or Min, then query again to find the corresponding object:
Movie mostAction = movies.MaxBy(m => m.NumSpaceBattles);