title: Call any one of a number of functions until we get a specified result
body: <p>I've got this ugly bit of code that looks a bit like this ...</p>
  
  <pre><code>TestResult GetFirstTestResult()
  {
      var result = TestMethod1();
      if(result is EmptyResult)
      {
          result = TestMethod2();
      }
  
      if(result is EmptyResult)
      {
          result = TestMethod3();
      }
  
      // ...
  
      if(result is EmptyResult)
      {
          result = TestMethodN();
      }
  
      return result;
  }
  </code></pre>
  
  <p>Basically, I need to run a number of tests until I find one that has some values.</p>
  
  <p>Now whilst the above code isn't pretty, for a small(ish) value of <code>N</code> is is managable. Sadly, in my case, <code>N</code> could get fairly big.</p>
  
  <p>Is there a way of writing this using a loop, something along the lines of this pseudo code...</p>
  
  <pre><code>TestResult GetFirstTestResult()
  {
      TestResult[] results = {TestMethod1(), TestMethod2(), TestMethod3(), .. TestMethodN()};
      return results.First(test=&gt;!test.Result is Emptyresult);
  }
  </code></pre>
  
  <p>such that each of the test methods were invoked <em>in the loop</em> so only the minimum number of them were actually executed?</p>
  
  <p><strong>EDIT</strong>
  With thanks to <a href="https://stackoverflow.com/users/885626/ric">Ric</a> and <a href="https://stackoverflow.com/users/622391/simon-m%E1%B6%9Ckenzie">Simon</a> I've got this...</p>
  
  <pre><code>TestResult GetFirstTestResult()
  {
      return new Func&lt;TestResult&gt;[]
          {
              TestMethod1,
              TestMethod2,
              TestMethodN
          }.Select(t =&gt; t())
                        .FirstOrDefault(r =&gt; !(r is EmptyResult)) ?? new EmptyResult();
  }
  </code></pre>
  
  <p>which calls each method in turn until it finds one where the result is not of type <code>EmptyResult</code> and returns that. If no such result is found it returns <code>new EmptyResult()</code></p>