LINQ is a program that was introduced as part of the .NET framework version 3.5 in 2007. LINQ allows developers to write efficient C# code using arrow functions to query collections of objects or from databases using libraries like Entity Framework.
In this tutorial, we are going to discuss top new LINQ features introduced in .NET 6.0.
- New methods MaxBy and MinBy
- New method Chunk
- Index and Range parameters
- New method TryGetNonEnumeratedCount
- Zip supports three IEnumerables.
MaxBy and MinBy
Assume we have the below collection:
using LINQIndotNET6.MinByAndMaxBy;
var siblings = new List < Siblings > () {
new Siblings("Praveen", 45),
new Siblings("Prashanth", 32),
new Siblings("Pramod", 28)
};
I need to find the youngest and oldest person from this list. How this can be achieved?
Old Approach
using LINQIndotNET6.MinByAndMaxBy;
var siblings = new List < Siblings > () {
new Siblings("Praveen", 45),
new Siblings("Prashanth", 32),
new Siblings("Pramod", 28)
};
// Old version Approach
var youngest = siblings.OrderBy(x => x.Age).First();
var Oldest = siblings.OrderByDescending(x => x.Age).First();
Console.WriteLine($ "Youngest Brother - Name: {youngest.Name} - Age :{youngest.Age}");
Console.WriteLine($ "Oldest Brother - Name: {Oldest.Name} - Age :{Oldest.Age}");
Console.ReadLine();
New Approach
using LINQIndotNET6.MinByAndMaxBy;
var siblings = new List < Siblings > () {
new Siblings("Praveen", 45),
new Siblings("Prashanth", 32),
new Siblings("Pramod", 28)
};
//New Approach
var youngestNew = siblings.MinBy(x => x.Age);
var OldestNew = siblings.MaxBy(x => x.Age);
Console.WriteLine("New Approach");
Console.WriteLine($ "Youngest Brother - Name: {youngestNew?.Name} - Age :{youngestNew?.Age}");
Console.WriteLine($ "Oldest Brother - Name: {OldestNew?.Name} - Age :{OldestNew?.Age}");
Console.ReadLine();
Chunk
Assume that I have the below list of names.
var names = new[] {"Paul","Peter","Michael","George","Mike","Buck"};
I need to split this into multiple lists, such as:
Paul, Peter, Michael
George, Mike, Buck
Old Approach
var names = new [] {
"Paul",
"Peter",
"Michael",
"George",
"Mike",
"Buck"
};
var chunkList = ChunkBy(names, 3).ToList();
var i = 1;
chunkList.ForEach(x => {
var item = string.Empty;
x.ToList().ForEach(chunk => {
item += chunk + ",";
});
Console.WriteLine($ "chunk-{i} - {item}");
i += 1;
});
IEnumerable < IEnumerable < T >> ChunkBy < T > (IEnumerable < T > source, int chunkSize) {
return source.Select((x, i) => new {
Index = i, Value = x
}).GroupBy(x => x.Index / chunkSize).Select(x => x.Select(y => y.Value));
}
Note
If you know anything better than the old approach, please feel free to add it in the comments section.
We have written multiple lines of code to split the list into different chunks. This was simplified in .NET 6.0 through the introduction of a new extension method called Chunk.
New Approach
var names = new [] {
"Paul",
"Peter",
"Michael",
"George",
"Mike",
"Buck"
};
var chunkList = names.Chunk(3).ToList();
var i = 1;
chunkList.ForEach(x => {
var item = string.Empty;
x.ToList().ForEach(chunk => {
item += chunk + ",";
});
Console.WriteLine($ "chunk-{i} - {item}");
i += 1;
});



Join the conversation! Your thoughts help the community grow.