title: Conditionally allowing for null in LINQ WHERE clause
body: <h2>Working:</h2>
  
  <p>I am trying to perform a LEFT OUTER JOIN between my <code>Contacts</code> table and my <code>Permissions</code> table. I have the basis of this working correctly, and get back a list of contacts regardless of whether they have a corresponding permission.</p>
  
  <pre class="lang-cs prettyprint-override"><code>// Query for Contacts
  from contact in Contacts
  join permission in Permissions on contact.Id equals permission.ObjectId into permissionGrp
  from p in permissionGrp.DefaultIfEmpty()            
  where p==null || (p!=null &amp;&amp; /* ... condition based on the existence of a permission */)
  select new { contact, permission = p };
  </code></pre>
  
  <p>Generated WHERE SQL:</p>
  
  <pre><code>WHERE
  (t1.PermissionId IS NULL OR 
      ((t1.PermissionId IS NOT NULL AND ... other conditions ... )))
  </code></pre>
  
  <h2>Problem:</h2>
  
  <p>I want to adapt the above to introduce 'fallback' check; Not working as expected.</p>
  
  <h2>Requirement:</h2>
  
  <ul>
  <li>When there is no <code>Permission</code> corresponding to the <code>Contact</code> (i.e. <code>p==null</code>) then only include the row based on the predetermined <code>bool</code> value of <code>allowed</code>.</li>
  </ul>
  
  <h2>Attempt:</h2>
  
  <p>I thought I could do <code>where (p==null &amp;&amp; allowed) || ...</code> like this: </p>
  
  <pre class="lang-cs prettyprint-override"><code>// Fallback permission
  bool allowed = false;
  
  // Query for Contacts
  from contact in Contacts
  join permission in Permissions on contact.Id equals permission.ObjectId into permissionGrp
  from p in permissionGrp.DefaultIfEmpty()
  
  /* Added bool condition 'allowed' to control inclusion when 'p' is null */
  where (p==null &amp;&amp; allowed) || (p!=null &amp;&amp; /* ... condition based on the existence of a permission */)
  select new { contact, permission = p };
  </code></pre>
  
  <h2>Expected:</h2>
  
  <h3>When <code>allowed = false</code> (don't accept <code>null</code> permission)</h3>
  
  <pre><code>WHERE
      ((t1.PermissionId IS NOT NULL AND ... other conditions ... ))
  </code></pre>
  
  <h3>When <code>allowed = true</code>  (accept <code>null</code> permission)</h3>
  
  <pre><code>WHERE
  (t1.PermissionId IS NULL OR 
      ((t1.PermissionId IS NOT NULL AND ... other conditions ... )))
  </code></pre>
  
  <h2>Actual Result:</h2>
  
  <p>Always outputs as if <code>allowed=false</code> even when <code>true</code>?</p>
  
  <pre><code>WHERE
      ((t1.PermissionId IS NOT NULL AND ... other conditions ... ))
  </code></pre>
  
  <h1>Summary:</h1>
  
  <p>I hope I am just doing something silly that's easily fixed.<br/>
  How can I filter my <code>null</code> value records based on a given <code>bool</code> value?</p>