Author : MD TAREQ HASSAN | Updated : 2021/03/22

What is LINQ?

There 2 types of LINQ syntax:

Consist of three distinct actions:

Query syntax

// 1. Data source
int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };

// 2. Query creation. numQuery is an IEnumerable<int>
var numQuery = from num in numbers
			   where (num % 2) == 0
			   select num;
			   
// 3. Query execution
foreach (int num in numQuery)
{

}

Method syntax

// 1. Data source
int[] numbers = { 5, 10, 8, 3, 6, 12};

// 2. Query creation. numQuery is an IEnumerable<int>
IEnumerable<int> numQuery = numbers.Where(num => num % 2 == 0).OrderBy(n => n);

// 3. Query execution
foreach (int i in numQuery)
{
    Console.Write(i + " ");
}