title: The || (or) Operator in Linq with C#
body: <p>I'm using linq to filter a selection of MessageItems. The method I've written accepts a bunch of parameters that might be null. If they are null, the criteria for the file should be ignored. If it is not null, use it to filter the results.</p>
  
  <p>It's my understanding that when doing an || operation is C#, if the first expression is true, the second expression should not be evaluated.</p>
  
  <p>e.g.</p>
  
  <pre><code>if(ExpressionOne() || ExpressionTwo())
  {
       // only ExpressionOne was evaluated because it was true
  }
  </code></pre>
  
  <p>now, in linq, I'm trying this:</p>
  
  <pre><code>var messages = (from msg in dc.MessageItems
  where  String.IsNullOrEmpty(fromname) || (!String.IsNullOrEmpty(fromname) &amp;&amp; msg.FromName.ToLower().Contains(fromname.ToLower()))
  select msg);
  </code></pre>
  
  <p>I would have thought this would be sound, because <code>String.IsNullOrEmpty(fromname)</code> would equal true and the second part of the || wouldn't get run.</p>
  
  <p>However it does get run, and the second part</p>
  
  <pre><code>msg.FromName.ToLower().Contains(fromname.ToLower()))
  </code></pre>
  
  <p>throws a null reference exception (because <code>fromname</code> is null)!! - I get a classic "Object reference not set to an instance of an object" exception.</p>
  
  <p>Any help?</p>