title: How to combine inner join and left join in Entity Framework
body: <p>I am using <code>DBContext</code> API from EF 4.1.
  Consider following entity model (A, B, E, D are entities)</p>
  
  <p>A: aId</p>
  
  <p>B: aId, cId</p>
  
  <p>E: eId, aId</p>
  
  <p>D: eId, cId, Data</p>
  
  <p>What I want is equivalent of below sql query</p>
  
  <pre><code>SELECT
     B.aId,
     B.cId,
     COALESCE(M.Data, [default value])
  FROM
     B LEFT OUTER JOIN
     (
        SELECT
           E.aId,
           D.cId,
           D.Data  
        FROM
           E INNER JOIN D ON E.eId = D.eId
     ) M
     ON B.aId = M.aId AND B.cId = M.cId
  </code></pre>
  
  <p>Its simple to have left join on B, E &amp; D but I found that I cannot solve above query. 
  I have tried linq form of what I think would be an equivalent query</p>
  
  <pre><code>// inner join equivalent
  var ee = db.E.Join(db.D, e =&gt; e.eId, d =&gt; d.eId,
      (e, d) =&gt; new { e.aId, e.eId, d.cId, d.Data });
  
  // left outer join
  var o = from c in db.B
          join e in ee on new { c.aId, c.cId }
              equals new { e.aId, e.cId } into temp
          from m in temp.DefaultIfEmpty()
          select new
          {
              c.aId,
              c.cId,
              Data = null != m ? m.Data : [default value]
          };
  </code></pre>
  
  <p>However, this fails when I call <code>o.ToString()</code> with following exception details:</p>
  
  <blockquote>
    <p>System.ArgumentException: The argument to DbIsNullExpression must
    refer to a primitive or reference type.    at
    System.Data.Common.CommandTrees.ExpressionBuilder.Internal.ArgumentValidation.ValidateIsNull(DbExpression
    argument, Boolean allowRowType)    at
    System.Data.Objects.ELinq.ExpressionConverter.EqualsTranslator.CreateIsNullExpression(ExpressionConverter
    parent, Expression input)    at
    System.Data.Objects.ELinq.ExpressionConverter.EqualsTranslator.TypedTranslate(ExpressionConverter
    parent, BinaryExpression linq)    at
    System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter
    parent, Expression linq)</p>
    
    <p>... [more stack trace out here]</p>
    
    <p>at
    System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression
    linq)    at System.Data.Objects.ELinq.ExpressionConverter.Convert()<br>
    at
    System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable<code>1
    forMergeOption)    at System.Data.Objects.ObjectQuery.ToTraceString() 
    at System.Data.Entity.Internal.Linq.InternalQuery</code>1.ToString()    at
    System.Data.Entity.Infrastructure.DbQuery`1.ToString()</p>
  </blockquote>
  
  <p>I have tried to form similar query using extension methods but had the same exception. What am I missing here?</p>
  
  <p><strong>---------------------------------------------------------------------------------</strong></p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>It appears that issue was due to line</p>
  
  <pre><code>Data = null != m ? m.Data : [default value]
  </code></pre>
  
  <p>I have modified it to </p>
  
  <pre><code>   Data = m
  </code></pre>
  
  <p>And it started working. I have to move null checking logic at the place where I am using the result. Now, I am wondering what can be the cause of exception? From exception details, it appears that it cannot figure out m (which is an anonymous type) as reference type. Is this behavior documented somewhere? </p>