Deffered Query Execution In Linq

Gopesh9
Posted by Gopesh9 under LINQ category on | Points: 40 | Views : 2087
Deffered Execution In Linq
var numbers = new List<int>();
numbers.Add (1);
IEnumerable<int> query = numbers.Select (n => n * 10); // Here I am Building a query
numbers.Add (2); // Now I am adding another element in the List
foreach(var item in numbers)
{
Console.WriteLine(" Number in the List : " + item);
}


If I will ask that what will be the output of this Linq query, then you all must think that since query is build before adding the second query, So the output must be 10.

But actually the output is
10
20
This is because here deffered execution took place.
When i am building the query, my execution doesn't take place but when I am doing any thing on that query the execution starts.

If you have any doubt about the Deffered Execution, feel free to ask..

Comments or Responses

Login to post response