Jack
Jack

Reputation: 3494

How do I convert this SQL outer join to LINQ

I want to convert this to Linq:

SELECT      {fields}
FROM        tableA AS A
LEFT JOIN   tableB AS B
ON             B.Field1 = MyVariable
AND            (    A.Key = B.Key
               OR   A.Key = B.AlternateKey)

It's that OR in conjunction with the AND clause that is tripping me over.

EDIT: Can I have the solution as extension methods please.

Upvotes: 0

Views: 45

Answers (1)

serges_newj
serges_newj

Reputation: 838

from a in tableA
  from b in tableB.Where(b => b.Field1 == MyVariable && (a.Key == b.Key || a.Key == b.AlternateKey)).DefaultIfEmpty()
  select new { a, b };

tableA.SelectMany(
  a => tableB.Where(b => b.Field1 == MyVariable && (a.Key == b.Key || a.Key == b.AlternateKey)).DefaultIfEmpty()
  .Select(b => new { a, b })
  );

Upvotes: 1

Related Questions