Author : Rahul
Last Modified : 15-Jul-2021
Complexity : Beginner
Query Execution Operator in LINQ
Query Execution Operator in LINQ is used to execute the LINQ query in different ways. Based on these two types of queries, There are two types of LINQ operators are available as follows.
- Deferred Operator
- Immediate Operator
Deferred Operator
In Deferred Execution, LINQ Query is not executed at the time when it is declared instead it will execute when we access the result of the query like in foreach loop. Operator select, SelectMany, where, Take, Skip are belongs to this category. Below are some examples in this category.
Query to show deferred execution.
class Program
{
static void Main(string[] args)
{
int[] numbers = { 4, 6, 8, 11, 12 };
//Linq Query
var result_Linq = from p in numbers where p < 10 select p;
//Lambda Expression
var result_Lambda = numbers.Where(p => p < 10);
//Original Array
Console.WriteLine("Original Array:");
for (int i=0; i<numbers.Length;i++)
{
Console.WriteLine(numbers[i]);
}
//Update array
Console.WriteLine("Updated Array:");
for (int i=0;i<numbers.Length;i++)
{
numbers[i] = numbers[i] - 3;
Console.WriteLine(numbers[i]);
}
//Display Linq Query Result after updating array
Console.WriteLine("Numbers less than 10 (Using Linq):");
foreach (int item in result_Linq)
{
Console.WriteLine(item);
}
//Display Lambda Expression Result after updating array
Console.WriteLine("Numbers less than 10 (Using Lambda):");
foreach (int item in result_Lambda)
{
Console.WriteLine(item);
}
Console.Read();
}
}
Output
Original Array:
4
6
8
11
12
Updated Array:
1
3
5
8
9
Numbers less than 10 (Using Linq):
1
3
5
8
9
Numbers less than 10 (Using Lambda):
1
3
5
8
9
Query to show deferred execution, query can be worked on new data.
class Program
{
static void Main(string[] args)
{
int[] numbers = { 4, 6, 8, 11, 12 };
//Linq Query
var result_Linq = from p in numbers where p < 10 select p;
//Lambda Expression
var result_Lambda = numbers.Where(p => p < 10);
//Original Array
Console.WriteLine("Original Array:");
for (int i=0; i<numbers.Length;i++)
{
Console.WriteLine(numbers[i]);
}
//Display Linq Query Result
Console.WriteLine("Numbers less than 10 (Using Linq):");
foreach (int item in result_Linq)
{
Console.WriteLine(item);
}
//Display Lambda Expression Result
Console.WriteLine("Numbers less than 10 (Using Lambda):");
foreach (int item in result_Lambda)
{
Console.WriteLine(item);
}
//Update array
Console.WriteLine("Updated Array:");
for (int i=0;i<numbers.Length;i++)
{
numbers[i] = numbers[i] - 3;
Console.WriteLine(numbers[i]);
}
//Display Linq Query Result after updating array
Console.WriteLine("Numbers less than 10 on updated array (Using Linq):");
foreach (int item in result_Linq)
{
Console.WriteLine(item);
}
//Display Lambda Expression Result after updating array
Console.WriteLine("Numbers less than 10 on updated array (Using Lambda):");
foreach (int item in result_Lambda)
{
Console.WriteLine(item);
}
Console.Read();
}
}
Output
Original Array:
4
6
8
11
12
Numbers less than 10 (Using Linq):
4
6
8
Numbers less than 10 (Using Lambda):
4
6
8
Updated Array:
1
3
5
8
9
Numbers less than 10 on updated array (Using Linq):
1
3
5
8
9
Numbers less than 10 on updated array (Using Lambda):
1
3
5
8
9
Immediate Operator
In Immediate Execution, LINQ Query is executed at the time when it is declared. Operator count, average, min, max, First, Last, ToArray, ToList belongs to this category. Below are some examples in this category.
Query to show immediate execution.
class Program
{
static void Main(string[] args)
{
int[] numbers = { 4, 6, 8, 11, 12 };
//Linq Query
var result_Linq = (from p in numbers where p < 10 select p).ToList();
//Lambda Expression
var result_Lambda = numbers.Where(p => p < 10).ToList();
//Original Array
Console.WriteLine("Original Array:");
for (int i=0; i<numbers.Length;i++)
{
Console.WriteLine(numbers[i]);
}
//Update array
Console.WriteLine("Updated Array:");
for (int i=0;i<numbers.Length;i++)
{
numbers[i] = numbers[i] - 3;
Console.WriteLine(numbers[i]);
}
//Display Linq Query Result after updating array
Console.WriteLine("Numbers less than 10 (Using Linq):");
foreach (int item in result_Linq)
{
Console.WriteLine(item);
}
//Display Lambda Expression Result after updating array
Console.WriteLine("Numbers less than 10 (Using Lambda):");
foreach (int item in result_Lambda)
{
Console.WriteLine(item);
}
Console.Read();
}
}
Output
Original Array:
4
6
8
11
12
Updated Array:
1
3
5
8
9
Numbers less than 10 (Using Linq):
4
6
8
Numbers less than 10 (Using Lambda):
4
6
8