I want to understand how chain query is processed. For example, let us consider the following query
var sumOfRoots = numbers //IEnum0
.Where(x => x > 0) //IEnum1
.Select(x => Math.Sqrt(x)) //IEnum2
.Select(x => Math.Exp(x)) //IEnum3
.Sum();
where e.g. numbers={-1, 4, 9 }.
Is this what happends behind the scene:
1. Getting all enumerators (forward pass)
numbers
callsGetEnumerator()
which returns (let us denote it with)IEnum0
instanceIEnum0
callsGetEnumerator()
which returnsIEnum1
instanceIEnum1
callsGetEnumerator()
which returnsIEnum2
instanceIEnum2
callsGetEnumerator()
which returnsIEnum3
instance
2. Calling MoveNext (backward pass)
.Sum()
callsMoveNext()
onIEnum3
IEnum3
callsMoveNext()
onIEnum2
IEnum2
callsMoveNext()
onIEnum1
IEnum1
callsMoveNext()
onIEnum0
3. Returning from MoveNext (forward-backward pass)
IEnum0
moves to element-1
and returntrue
.IEnum1
check if-1
satisfy condition (which is not true) soIEnum1
callsMoveNext()
onIEnum0
.IEnum0
moves to element4
and returntrue
.IEnum1
check if4
satisfy condition (which is true) and returnstrue
IEnum2
does nothing, just return output ofIEnum1
which istrue
IEnum2
does nothing, just return output ofIEnum2
which istrue
4. Calling Current (backward pass)
.Sum()
callsCurrent
onIEnum3
.IEnum3
callsCurrent
onIEnum2
IEnum2
callsCurrent
onIEnum1
IEnum1
callsCurrent
onIEnum0
5. Returning Current (forward pass)
IEnum0
returns4
IEnum1
returns4
IEnum2
returnssqrt(4)=2
IEnum3
returnsexp(2)
6. Repeat steps 2.-5. until step 3. returns false
Please correct me if a chain query is processed in a different way.