C# language: LINQ

LINQ is a query language used for quickly and efficiently creating queries for objects, collections, arrays, databases, generally speaking, various objects, LINQ is similar to SQL, first we enter a condition, then operate on a database or data collection, let’s go to the examples.

Basics

Where and Select

Take a look at the example below:

As you can see LINQ queries resemble SQL, the result of this program will be Micheal, we create a LINQ query that pulls out all elements from the table that have four characters or more, we insert the result into a generic list and display it in the foreach loop.

As you can see there is no select word here, the same where in dotted notation with the condition it knows straight away that it has to select values.

This is a notation with a dot, the standard notation looks like this:

In the above example, the standard notation must already be the word select.

In this lesson we will from time to time use dot notation and sometimes standard notation, if you want you can change these notations, I will not make two notations with each example because you can do it very easily and this lesson would been too long.

 

OrderBy

In LINQ, OrderBy orders the data ascending, see the example below:

And to sort numbers by descending, just enter OrderByDescending.

In this query there is no “Where” condition in you have to use the “Select” instruction.

 

GroupBy

GroupBy returns values, according to real and false conditions by which grouping occurs, look at the example below:

The result will be:

Of course, we create a condition in GroupBy and display the returned data in the foreach but we have this time we have two loops in the first we display the flag true, false and in the second loop of values consistent with those flag.

 

Join

The word join joins the same data from both collections, i.e. if in both collections, for example, are the same numbers so are extracted from the collection, look at the example below:

In both arrays, only one number is the same, is it 22 number, so we join it and in the console will display the number 22, in the query the word “on” acts as “if” and “equals” acts as a condition, i.e. if the same value in the array numbers1 is in the numbers2 array, pull it out.

 

SelectMany

This instruction is a very interesting solution, it allows us not only to extract some value or object, but also to analyze it, look at the example below:

Result:

Also, you can use this instruction in the same way to analyze objects, so I recommend to test 🙂

 

Concat

This very simple instruction simply serves to combine in our case whole arrays

I do not think I need to explain anything here, let’s continue.

 

Zip

The zip statement is used to perform some operations, eg on arrays without data pulling out, for example:

In the console there will be results in turn: 18, 20, 63, which means you have
we multiplied both tables by yourself.

This content also you can find on my steemit blog: https://steemit.com/programming/@slawas/c-language-linq

And on medium: https://medium.com/@sawomirkowalski/c-language-linq-3e958f30f67d

It was a long lesson, but we made it to the end. Remember, if you do not use LINQ or any technology you learn in a practical project, you will not remember much, only the knowledge you use remains in the mind.

 
If that post was useful for you share it with your friends :)

Post a comment

Be first!

avatar
  Subscribe  
Notify about