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:
static void Main(string[] args)
{
string[] names = { "Nick", "Micheal", "Paul" };
IEnumerable<string> filteredNames = names.Where(n => n.Length >= 5);
foreach (string n in filteredNames)
Console.WriteLine(n);
//Micheal
Console.ReadKey();
}
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:
static void Main(string[] args)
{
string[] names = { "Nick", "Micheal", "Paul" };
//IEnumerable<string> filteredNames = names.Where( n => n.Length >= 5);
var filteredNames = from n in names
where n.Length >= 5
select n;
foreach (string n in filteredNames)
Console.WriteLine(n);
//Micheal
Console.ReadKey();
}
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:
static void Main(string[] args)
{
int[] numbers= {1,22,3,4,53,6,7,78,9};
IEnumerable<int> OrderNumbers= numbers.Select(n=>n).OrderBy(n=>n);
foreach (int number in OrderNumbers)
Console.WriteLine(number);
Console.ReadKey();
}
And to sort numbers by descending, just enter OrderByDescending.
IEnumerable<int> filteredNames = names.Select(n=>n).OrderByDescending(n=>n);
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:
static void Main(string[] args)
{
int[] numbers = {1,22,3,4,53,6,7,78,9};
var filterdNumbers = numbers.Select(n=>n).GroupBy(n => n>22 );
foreach (var bigger in filterdNumbers)
{
Console.WriteLine(bigger.Key);
foreach (var number in bigger)
{
Console.WriteLine(number);
}
}
Console.ReadKey();
}
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:
static void Main(string[] args)
{
int[] numbers1 = {1, 22, 3, 4, 53, 6, 7, 78, 9};
int[] numbers2 = { 15, 22, 32, 477, 535, 65, 73, 785, 93};
var filterdNumbers = from num1 in numbers1
join num2 in numbers2 on (num1) equals num2
select num1;
foreach (var number in filterdNumbers)
{
Console.WriteLine(number);
}
Console.ReadKey();
}
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:
static void Main(string[] args)
{
string[] text = { "some", "test", "text" };
var chars = text.SelectMany(key => key.ToCharArray());
foreach (var item in chars)
{
Console.Write(item + " ");
}
Console.ReadKey();
}
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
static void Main(string[] args)
{
string[] text1 = { "some", "test" };
string[] text2 = { "text" };
var text = text1.Concat(text2);
foreach (var t in text)
{
Console.Write(t);
}
Console.ReadKey();
}
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:
static void Main(string[] args)
{
int[] numbers1 = {3,4,7};
int[] numbers2 = {6,5,9};
var numbers = numbers1.Zip(numbers2, (a, b) => a * b);
foreach (var number in numbers)
{
Console.WriteLine(number);
}
Console.ReadKey();
}
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 blog http://devman.pl/csharplan/c-language-linq/
If you recognise it as useful, share it with others so that others can also use it.
Leave upvote and follow and wait for next articles :) .
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.