P M A J E W S K I

Please Wait For Loading

Unlocking the Power of LINQ: 7 Rarely Used Queries You Should Know - Software Developer's Tour

    You Are Currently Here!
  • Home
  • ProgrammingUnlocking the Power of LINQ: 7 Rarely Used Queries You Should Know
LINQ

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

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

int[] array1 = [1, 2, 3, 4];
int[] array2 = [1, 2];

foreach (int i in array1.Intersect(array2))
{
    Console.Write($"{i}, ");
}

And the output is: 1, 2,

3. Except

Except returns elements from first collection, not existing in the second one

int[] array1 = [1, 2, 3, 4];
int[] array2 = [1, 2];

foreach (int i in array1.Except(array2))
{
    Console.Write($"{i}, ");
}

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.

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.

int[] array1 = [5, 1, 2, 3, 4, 1, 2];
int[] array2 = [0, 1, 2];

foreach (var i in array1.Zip(array2, (x, y) => $"[{x}] = {y}"))
{
    Console.Write($"{i}, ");
}

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.

int[] array1 = [1, 2];

foreach (var i in array1.Prepend(3))
{
    Console.Write($"{i}, ");
}

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.

Car[] array1 = [new Car("Name1", "AwesomeBrand"), new Car("Name2", "AwesomeBrand"), new Car("Name3", "AwesomeBrand"), new Car("Name4", "NormalBrand")];

foreach (var i in array1.ToLookup(x => x.Brand))
{
    Console.WriteLine($"{i.Key}: {string.Join(',', i.Select(x => x.Name))}");
}

record Car(string Name, string Brand);

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.

leave a comment