After reading various LINQ tutorials I am wondering how exactly it works under the hood and I am hoping someone could confirm some of the questions I have with the following example code:
// The Three Parts of a LINQ Query:
// 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)
{
Console.Write("{0,1} ", num);
}
My questions refer to part 2:
With question 2 my assumption would be that the object generated from the LINQ query produces code within the GetEnumerator function which retrieves the data in a fashion represented by our LINQ syntax. Just my guess... Unless I'm thinking about LINQ completely wrong...
Both of your questions are closely related.
So, yes, there is a difference. LINQ query operator return decorator sequences and this decoration enables deferred execution . Decorator sequence is a wrapper over another sequence ( int[]
in your case) that also knows which query to apply on wrapped sequence. Actual execution occurs at GetEnumerator().MoveNext()
call.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.