Chunking
Chunking is a concept in which the sequence of elements are split into different portions of mentioned size.
Example
I have a list of string object which holds the list of cities.
var bigCities = new List<string>() { "New York", "London", "Mumbai", "Chicago", "Sydney", "Toronto" };
Now I want to split the object into parts with size of three.
Generally we end up writing a complicated LINQ query to do this step
int chunkSize = 3;
var chunkedOutput = bigCities.Select((x,i) => new { Index=i,value =x })
.GroupBy(x=>x.Index/chunkSize).Select(x=>x.Select(m=>m.value));
The above query will return me the output where the result set will be split into two parts with size 3.
The same process can be done simply by using a method which is available in .NET 6.
var chunkedOutput = bigCities.Chunk(3);
public static System.Collections.Generic.IEnumerable<TSource[]> Chunk<TSource>(this System.Collections.Generic.IEnumerable<TSource> source, int size);
The Maximum of each chunk is Int32.
var bigCities = new List < string > () {
"New York",
"London",
"Mumbai",
"Chicago",
"Sydney",
"Toronto"
};
//In Previous Versions of Dotnet
int chunkSize = 3;
var chunkedOutput = bigCities.Select((x, i) => new {
Index = i, value = x
}).GroupBy(x => x.Index / chunkSize).Select(x => x.Select(m => m.value));
//In .Net 6
var chunked = bigCities.Chunk(2);

Getting Count without Enumeration
var bigCities = new List<string>() { "New York", "London", "Mumbai", "Chicago", "Sydney", "Toronto" }.AsEnumerable();
bigCities.Count();
In the above code, If I want to find the count of bigCities object by using Count() method, it will make an enumeration.
But in .Net6 we can achieve this without making an enumeration.
bigCities.TryGetNonEnumeratedCount(out int count);
By using this TryGetNonEnumeratedCount we can determine the number of elements in a sequence without forcing an Enumeration.
Below is the code for this TryGetNonEnumeratedCount() Method.
// Summary:
// Attempts to determine the number of elements in a sequence without forcing an
// enumeration.
//
// Parameters:
// source:
// A sequence that contains elements to be counted.
//
// count:
// When this method returns, contains the count of source if successful, or zero
// if the method failed to determine the count.
//
// Type parameters:
// TSource:
// The type of the elements of source.
//
// Returns:
// true if the count of source can be determined without enumeration; otherwise,
// false.
public static bool TryGetNonEnumeratedCount < TSource > (this IEnumerable < TSource > source, out int count) {
throw null;
}
Zipping
A sequence of tuples with elements taken from the first, second, and third sequences, in that order.
We are having three sequences as given below
var playerName = new string[] { "Dhoni", "Kohli", "Sachin", "Ganguly" };
var jerseryNumber = new int[] { 7, 18, 10, 99 };
var place = new string[] { "Ranchi", "Delhi", "Mumbai", "Kolkata" };



Alexander BurachevskyPosted Feb 22, 2022, 4:27 PM
A good simple editor for C# code and LINQ queries. Supports MsSql, MySql, Sqlite, MsAccess, SqlCe,syntax highlighting, IntelliSense.Built on .NET6. Allows you to connect your own assemblies (NET5+) https://www.microsoft.com/en-us/store/r/linqcode/9p26rgmfp42b?rtc=1&wa=wsignin1.0&activetab=pivot:overviewtab