title: Sequence contains no elements error but I want to check for null
body: <p>I have the following problem:</p>
  
  <pre><code>public Boolean Exists(String userName)
  {
      IRepository&lt;User&gt; = new UserRepository();
      User user = userRepository.First(u =&gt; u.Name == userName);
  
      if (user == null) return false;
  
      // Exists!
      return true;
  }
  </code></pre>
  
  <p>The problem is now, that I can't check the User object for null. Before I get there, I get an <code>InvalidOperationException</code> saying "<strong>The sequence contains no elements</strong>".</p>
  
  <p>This seems really weird to me, especially as I don't want to establish control flow with exceptions (e.g. encapsulate in try..catch and return true/false in the respective parts).</p>
  
  <p>What's going on here? Is this normal or is there something wrong with my respository (hint?!)</p>
  
  <p><strong>By the way, this code works <em>perfectly</em> when the element that I'm looking for exists (the User is retrieved etc.). It only doesn't work when there is no match.</strong></p>