Do you know how the GroupBy operator is fetching the lazy-evaluated data?
Let me show you. This helper function will log each item before producing it.
We want to subject lazy-evaluated numbers to the GroupBy operator, but only to fetch the first group.
How many numbers will GroupBy pull?
And if I only wanted to fetch the first item from the first group, will that not pull only one item?
Unfortunately, no.
Since any element might fit into the first group, including the last one, GroupBy will pull the entire sequence only to populate the first group.
We wanted a single value, but the operator has pulled the entire sequence.
While GroupBy operator is deferred, its execution is still eager. GroupBy is an eager deferred operator.
#linq #dotnet #csharp