title: Linq to SQL left outer join using Lambda syntax and joining on 2 columns (composite join key)
body: <p>I am trying to make an Inner Join on 2 columns with Linq to SQL as a Lambda expression. The normal query would look like this.</p>
  
  <pre><code>SELECT * FROM participants 
  LEFT OUTER JOIN prereg_participants ON prereg_participants.barcode = participants.barcode
  AND participants.event_id = prereg_participants.event_id
  WHERE (participants.event_id = 123)
  </code></pre>
  
  <p>I am succeeding in making a Left Outer Join on one column with the following code.</p>
  
  <pre><code>var dnrs = context.participants.GroupJoin(
      context.prereg_participants,
      x =&gt; x.barcode,
      y =&gt; y.barcode,
      (x, y) =&gt; new { deelnr = x, vi = y })
      .SelectMany(
      x =&gt; x.vi.DefaultIfEmpty(),
      (x, y) =&gt; new { deelnr = x, vi = y })
      .Where(x =&gt; x.deelnr.deelnr.event_id == 123)
      .ToList();
  </code></pre>
  
  <p>The problem is that with the above Lambda I get too many results because it is missing the <code>AND participants.event_id = prereg_participants.event_id</code> part. But whatever I try i'm not getting the correct amount of participants. </p>
  
  <p>I looked at the following existing questions, but none solved my problem in writing the correct lambda. And most of the solutions are nog in lambda-format or not a Left outer join on multiple columns. </p>
  
  <p><a href="https://stackoverflow.com/questions/373541/how-to-do-joins-in-linq-on-multiple-fields-in-single-join">How to do joins in LINQ on multiple fields in single join</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1122942/linq-to-sql-left-outer-join-with-multiple-join-conditions">LINQ to SQL - Left Outer Join with multiple join conditions</a></p>
  
  <p><a href="https://stackoverflow.com/questions/22089261/group-by-using-more-than-two-columns-by-lambda-expression">Group By using more than two columns by Lambda expression</a></p>
  
  <p>And most of these from <a href="https://www.google.nl/search?source=hp&amp;ei=SM4DW5AQxr1R5L6z4Ag&amp;q=linq+to+sql+multitple+columns+lambda+site%3Astackoverflow.com&amp;oq=linq+to+sql+multitple+columns+lambda+site%3Astackoverflow.com&amp;gs_l=psy-ab.3...879.11821.0.11997.61.55.1.5.5.0.154.4649.42j12.54.0....0...1c.1.64.psy-ab..1.43.3445...0j0i131k1j0i10k1j0i22i30k1j0i22i10i30k1j0i13k1j33i160k1j0i8i13i30k1.0.teKgRX0evgc" rel="nofollow noreferrer">this Google search</a></p>