title: LINQ's deferred execution, but how?
body: <p>This must be something really simple. But i'm going to ask it anyway, because i think that others will also struggle with it. Why does following simple LINQ query is not executed always with the new variable-value instead of always using the first?</p>
  
  <pre><code>static void Main(string[] args)
  {
      Console.WriteLine("Enter something:");
      string input = Console.ReadLine();       // for example ABC123
      var digits = input.Where(Char.IsDigit);  // 123
      while (digits.Any())
      {
          Console.WriteLine("Enter a string which doesn't contain digits");
          input = Console.ReadLine();         // for example ABC
      }
      Console.WriteLine("Bye");
      Console.ReadLine();
  }
  </code></pre>
  
  <p>In the commented sample it will enter the loop since the input <code>ABC123</code> contains digits. But it will never leave it even if you enter something like <code>ABC</code> since <code>digits</code> still is <code>123</code>.</p>
  
  <p>So why does the LINQ query not evaluate the new <code>input</code>-value but always the first? </p>
  
  <p>I know i could fix it with this additional line:</p>
  
  <pre><code>while (digits.Any())
  {
      Console.WriteLine("Enter a string which doesn't contain digits");
      input = Console.ReadLine();          
      digits = input.Where(Char.IsDigit);  // now it works as expected
  }
  </code></pre>
  
  <p>or - more elegant - by using the query directly in the loop:</p>
  
  <pre><code>while (input.Any(Char.IsDigit))
  {
      // ...
  }
  </code></pre>