title: Linq-Sql IQueryable<T> and chaining OR operations
body: <p>I'm trying to simulate:</p>
  
  <p>WHERE x.IsActive = true OR x.Id = 5</p>
  
  <p>The following causes 'AND' to be used... how do I simulate an 'OR' condition with IQueryable (qry) and my nullable int, given that other filtering might be involved as with the IsActive filter here?</p>
  
  <pre><code>            if (onlyActiveItems) //bool
              {
                  qry = qry.Where(x =&gt; x.IsActive == true);
              }
  
              if (whenSpecifiedMustIncludeRecordWithThisId.HasValue) //int?
              {
                  qry = qry.Where(x =&gt; x.Id == whenSpecifiedMustIncludeRecordWithThisId.Value);
              }
  </code></pre>
  
  <p>I have considered union but its seems the answer to this should be much simpler.</p>
  
  <hr>
  
  <p>This is one solution which gets around the problem I get with "Nullable object must have a value" when trying the combined all in one answer.  What causes the nullable to be evaluated when it is null otherwise?</p>
  
  <pre><code>            if (whenSpecifiedMustIncludeRecordWithThisId.HasValue)
              {
                  qry = qry.Where(x =&gt; (!onlyActiveItems || (onlyActiveItems &amp;&amp; x.IsActive)) || x.Id == whenSpecifiedMustIncludeRecordWithThisId.Value);
              }
              else
              {
                  qry = qry.Where(x =&gt; (!onlyActiveItems || (onlyActiveItems &amp;&amp; x.IsActive)));
              }
  </code></pre>
  
  <p>It seems also in some cases the use of the nullable's .Value property makes a difference as seen in another question of mine here <a href="https://stackoverflow.com/questions/1634992/linq-to-sql-int16-gets-converted-as-int32-in-sql-command">Linq to SQL Int16 Gets Converted as Int32 In SQL Command</a></p>