title: Linq query from list containing check for null condition
body: <p>I am trying to use a Linq query to search for a match in a giant list (around 100k items). The search criteria needs to be the fields <code>StateCode</code>, <code>ChannelCode</code>, <code>EndDate</code>, and <code>ZipCode</code>. </p>
  
  <p>However, the check for <code>ZipCode</code> is a little tricky. I need to pull back results given either of these two conditions: </p>
  
  <ul>
  <li>If <code>StateCode</code>, <code>ChannelCode</code>, and <code>EndDate</code> are a match and the matching record in the list has a <code>null</code> value for <code>ZipCode</code>. </li>
  <li>If <code>StateCode</code>, <code>ChannelCode</code>, <code>EndDate</code> and <code>ZipCode</code> are a match, with <code>ZipCode</code> being a defined value in both the list and comparison items</li>
  </ul>
  
  <p>This is the query as I have it right now... which doesn't work. It constantly pulls back a list with a count of <code>0</code>. However, if I remove the section that searches for the <code>ZipCode</code> match, then I will get results. </p>
  
  <pre><code>List&lt;ZipCodeTerritory&gt; previousZips = allRecords.Where(
          z =&gt; (z.StateCode.Equals(item.StateCode) &amp;&amp;
          z.ChannelCode.Equals(item.ChannelCode) &amp;&amp;
          z.EndDate.Date == item.EndDate.Date &amp;&amp;
          (z.ZipCode.Equals(null) | z.ZipCode.Equals(item.ZipCode))
      )).ToList();
  </code></pre>
  
  <p>I have also tried replacing <code>z.ZipCode.Equals(null)</code> with <code>string.IsNullOrWhiteSpace(z.ZipCode)</code> and this still didn't work. </p>
  
  <p><strong>EDIT:</strong> I have also attempted the condition with <code>|</code> and <code>||</code> as the <code>or</code> operator. Neither work...</p>
  
  <p>Just in case, here is the query written out into a SQL statement. This works as expected if I run it in SQL Server Management Studio. </p>
  
  <pre><code>SELECT * FROM ZipCodeTerritory 
  WHERE StateCode = 'OR ' and ChannelCode = 'G' and EndDate = '12/31/9999' and (ZipCode IS NULL OR ZipCode = '00001')
  </code></pre>
  
  <p><strong>SECOND EDIT</strong></p>
  
  <p>This is how I'm grabbing the record from the database. Again, since I didn't want to have to make multiple calls I wanted to grab all the record from the <code>ZipCodeTerritory</code> table and then search through that list. </p>
  
  <pre><code>List&lt;ZipCodeTerritory&gt; allRecords = (from z in db.ZipCodeTerritory
                                          select z).ToList();
  </code></pre>