Today, I would like to talk about LINQ. LINQ queries are ubiquitous in the C# world, whether you’re working with WebAPI, MVC, or Windows Forms applications.
But honestly, we are using only a small part of the LINQ methods.
Where,Select, OrderBy, ThenBy / ThenByDescending, GroupBy, Join, First, FirstOrDefault, Single, SingleOrDefault, Count, Any, All and some aggregate functions for sequence of numeric values like Sum, Min, Max, Average.
Rarely used queries
Let’s look on less known methods.
1. Distinct
Distinct exactly like in SQL selects unique elements
int[] array = [1, 2, 3, 1, 2];
foreach(int i in array.Distinct())
{
Console.Write($"{i}, ");
}
And the output is: 1, 2, 3,
2. Intersect
Intersect returns elements existing in both collections
SkipWhile checks the condition for each element in the sequence. When element does not match this predicate, filtering stops, and the rest of the sequence is returned.
int[] array1 = [1, 2, 3, 4, 1, 2];
foreach (int i in array1.SkipWhile(x => x < 3))
{
Console.Write($"{i}, ");
}
And the output is: 3, 4, 1, 2,
but
int[] array1 = [5, 1, 2, 3, 4, 1, 2];
foreach (int i in array1.SkipWhile(x => x < 3))
{
Console.Write($"{i}, ");
}
And the output is: 5, 1, 2, 3, 4, 1, 2,
Because 5 doesn’t match condition “x < 3” and filtering has stopped.
5. Zip
Zip Combines two or more sequences into a single one, output collection length will be the smallest input collection.
Unlocking the Power of LINQ: 7 Rarely Used Queries You Should Know
Today, I would like to talk about LINQ. LINQ queries are ubiquitous in the C# world, whether you’re working with WebAPI, MVC, or Windows Forms applications.
But honestly, we are using only a small part of the LINQ methods.
Where, Select, OrderBy, ThenBy / ThenByDescending, GroupBy, Join, First, FirstOrDefault, Single, SingleOrDefault, Count, Any, All and some aggregate functions for sequence of numeric values like Sum, Min, Max, Average.
Rarely used queries
Let’s look on less known methods.
1. Distinct
Distinct exactly like in SQL selects unique elements
And the output is: 1, 2, 3,
2. Intersect
Intersect returns elements existing in both collections
And the output is: 1, 2,
3. Except
Except returns elements from first collection, not existing in the second one
And the output is: 3, 4,
4. SkipWhile
SkipWhile checks the condition for each element in the sequence. When element does not match this predicate, filtering stops, and the rest of the sequence is returned.
And the output is: 3, 4, 1, 2,
but
And the output is: 5, 1, 2, 3, 4, 1, 2,
Because 5 doesn’t match condition “x < 3” and filtering has stopped.
5. Zip
Zip Combines two or more sequences into a single one, output collection length will be the smallest input collection.
And the output is: [5] = 0, [1] = 1, [2] = 2,
6. Prepend
Prepend is similar to Append, but it adds an element at the beginning.
And the output is: 3, 1, 2,
7. ToLookup
ToLookup is one of the most interesting LINQ method for me. It allows us to create groups just like GroupBy, but with immediate execution.
And the output is:
AwesomeBrand: Name1,Name2,Name3
NormalBrand: Name4
I wanted highlight these seven useful yet often forgotten methods in LINQ, even though there are many more worth exploring.
Archives
Understanding K-Nearest-Neighbors (KNN) – Essential Machine Learning Algorithm
2024-10-06A Step-by-Step Guide to Web Scraping for Beginners
2024-07-12Categories
Meta