Why does LINQ not cache enumerations?
<p>So it is my understanding that LINQ does not execute everything immediately, it simply stores information to get at the data. So if you do a <code>Where</code>, nothing actually happens to the list, you just get an <code>IEnumerable</code> that has the information it needs to become the list.</p>

<p>One can 'collapse' this information to an actual list by calling <code>ToList</code>.</p>

<p>Now I am wondering, why would the LINQ team implement it like this? It is pretty easy to add a <code>List</code> at each step (or a <code>Dictionary</code>) to cache the results that have already been calculated, so I guess there must be a good reason.</p>

<p>This can be checked by this code:</p>

<pre><code>var list = Enumerable.Range(1, 10).Where(i =&gt; {
    Console.WriteLine("Enumerating: " + i);
    return true;
});

var list2 = list.All(i =&gt; {
    return true;
});

var list3 = list.Any(i =&gt; {
    return false;
});
</code></pre>

<p>If the cache were there, it would only output the <code>Enumerating: i</code> once for each number, it would get the items from the cache the second time.</p>

<p><strong>Edit:</strong> Additional question, why does LINQ not include a cache option? Like <code>.Cache()</code> to cache the result of the previous enumerable?</p>