title: Simple conditional LINQ query in Entity Framework 4
body: <p>I have recently migrated this code to Entity Framework 4 and it is failing. Obviously if status does not have a value, return all matches, if it does have a value match ones where <code>user.StatusID == 1</code>.</p>
  
  <pre><code>return users.SingleOrDefault(
                  user =&gt;
                  user.Username == username &amp;&amp;
                  user.EncryptedPassword == password &amp;&amp;
                  (!status.HasValue || user.StatusID == 1)
                  );
  </code></pre>
  
  <p>Exception returned:</p>
  
  <pre><code>ArgumentException: The specified value is not an instance of type 'Edm.Int32'
  Parameter name: value
  </code></pre>
  
  <p>However, removing the conditional test and it works fine:</p>
  
  <pre><code>return users.SingleOrDefault(
                  user =&gt;
                  user.Username == username &amp;&amp;
                  user.EncryptedPassword == password &amp;&amp;
                  user.StatusID == 1
                  );
  </code></pre>
  
  <p>Any ideas? How do you perform conditional testing in EF 4? Surely not separate if lines?</p>
  
  <p>I use these conditional tests time and time again in Linq to Sql; it is really odd as to why this is not functioning in EF 4. There must be something simple going wrong, perhaps there is a recommended alternate way of doing things in EF 4.0.</p>
  
  <p>Thanks for your help guys,<br/>
  Graham</p>