case1
var numbers = new List<int>();
numbers.Add (1);
IEnumerable<int> query = numbers.Select (n => n * 10); // Build query
numbers.Add (2);
//Use or execute query
case2
var numbers = new List<int>() { 1, 2 };
numbers.Add(4);
List<int> query = numbers
.Select (n => n * 10)
.ToList(); // Executes immediately into a List<int>
numbers.Add(3);
numbers.Clear();
//Use or execute query
Why in the first case query contains both 1,2
In second case query contains only 1,2,4 but not 3,is it because we are calling .ToList() method.
It’s because the query is not executed until you start enumerating over the resultset (by either calling .ToArray(), .ToList(), or simply write a foreach)
doesn’t execute anything. It’s the the lazy nature of LINQ.