title: Entity Framework join with null condition
body: <p>I have a SQL query that I'm trying to convert to an Entity Framework expression. One of the join clauses has a test for null and I'm not sure how to express that in EF. Here is the SQL query, notice that the join actually has multiple conditions:</p>
  
  <pre><code>SELECT T1.* 
  FROM product T1
  INNER JOIN T2 ON T1.pk1 = T2.fk1
  INNER JOIN T3 ON T2.pk2 = T3.fk2 AND T3.x IS NULL     &lt;-- this is the issue
  </code></pre>
  
  <p>Out of blind hope for something simple, I tried this EF expression:</p>
  
  <pre><code>from t1 in ctx.T1
  join t2 in ctx.T2 on t1.pk1 equals t2.fk1
  join t3 in ctx.T3 on t2.pk2 equals t3.fk2 &amp;&amp; t3.x == null
  select t1
  </code></pre>
  
  <p>The compiler doesn't like the <code>&amp;&amp; t3.x == null</code> part. It says</p>
  
  <blockquote>
    <p>Error CS0019 Operator '&amp;&amp;' cannot be applied to operands of type 'int' and 'bool'</p>
  </blockquote>
  
  <p>So I tried this (note that T3.x is a nullable date field):</p>
  
  <pre><code>from t1 in ctx.T1
  join t2 in ctx.T2 on t1.pk1 equals t2.fk1
  join t3 in ctx.T3 on new { A = t2.pk2, B = null as DateTime? } equals new { A = t3.fk2 &amp;&amp; B = t3.x }
  select t1
  </code></pre>
  
  <p>which compiles, but when I run it I get</p>
  
  <blockquote>
    <p>Unable to create a null constant value of type 'System.Object'. Only entity types, enumeration types or primitive types are supported in this context.</p>
  </blockquote>
  
  <p>So how might I go about this?</p>
  
  <p><strong>Update</strong>: as Ivan Stoev pointed out, the issue was that I was using <code>null as DateTime?</code> rather than <code>(DateTime?)null</code>. So the working query looks like this:</p>
  
  <pre><code>from t1 in ctx.T1
  join t2 in ctx.T2 on t1.pk1 equals t2.fk1
  join t3 in ctx.T3 on new { A = t2.pk2, B = (DateTime?)null } equals new { A = t3.fk2 &amp;&amp; B = t3.x }
  select t1
  </code></pre>