title: Linq Lambda multiple tables ( 4 tables ) LEFT JOIN
body: <p>I have 4 tables;</p>
  
  <pre><code>TMain &gt;&gt; MainId (PK)
  
  T1 &gt;&gt; T1_Id, MainId, X (PK and FK) X is decimal
  
  T2 &gt;&gt; T2_Id, MainId, X (PK and FK) X is decimal
  
  T3 &gt;&gt; T3_Id, MainId, X (PK and FK) X is decimal
  </code></pre>
  
  <p>Here SQL output;</p>
  
  <pre><code>SELECT TMain.*, (ISNULL(T1.X,0) + ISNULL(T2.X,0) + ISNULL(T3.X,0)) AS TOTAL FROM TMain
  
  LEFT OUTER JOIN T1 ON TMain.MainId = T1.MainId
  
  LEFT OUTER JOIN T2 ON TMain.MainId = T2.MainId
  
  LEFT OUTER JOIN T3 ON TMain.MainId = T3.MainId
  </code></pre>
  
  <p>How can I write it <strong>LINQ LAMDA</strong></p>
  
  <pre><code>   var AbbA = MyContext.TMain
                      .GroupJoin(
                          MyContext.T1,
                          q1 =&gt; q1.TMainId,
                          q2 =&gt; q2.TMainId,
                          (x, y) =&gt; new { A = x, T1_A = y })
                              .SelectMany(
                              xy =&gt; xy.T1_A.DefaultIfEmpty(),
                              (x, y) =&gt; new { A = x.A, T1_A = y })
                      .GroupJoin(
                          MyContext.T2,
                          q1 =&gt; q1.A.TMainId,
                          q2 =&gt; q2.TMainId,
                          (x, y) =&gt; new { A = x, T2_A = y })
                              .SelectMany(
                              xy =&gt; xy.T2_A.DefaultIfEmpty(),
                              (x, y) =&gt; new { A = x.A, T2_A = y })
                      .GroupJoin(
                          MyContext.T3,
                          q1 =&gt; q1.A.A.TMainId,
                          q2 =&gt; q2.TMainId,
                          (x, y) =&gt; new { A = x, T3_A = y })
                              .SelectMany(
                              xy =&gt; xy.T3_A.DefaultIfEmpty(),
                              (x, y) =&gt; new { A = x.A, T3_A = y })
                      .Select(q =&gt; new 
                      {
                          TMainId = q.A.A.A.TMainId,
                          Total = (q.T3_A.X == null ? 0 : q.T3_A.X) +
                                  (q.A.T2_A.X == null ? 0 : q.A.T2_A.X) +
                                  (q.A.A.T1_A.X == null ? 0 : q.A.A.T1_A.X),
                      }).ToList();
  </code></pre>
  
  <p>So I want to access T1 fields or TMain fields </p>
  
  <p>I wrote q.A.A.T1_A.X or q.A.A.A. in linq select</p>
  
  <p>Is that true? or have simplest way?</p>