Linq to SQL转换为SQL语法

时间:2012-04-04 11:53:20

标签: c# sql linq linq-to-sql

我正在尝试在Linq中执行以下查询

SELECT *
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.ID = T2.AnotherID
LEFT OUTER JOIN Table3 T3
on T1.ID = T3.AnotherID

它在正确的SQL语法中按预期工作,但我很难将其转换为相应的Linq to SQL语法。

如何将左连接与内连接组合?

此致

1 个答案:

答案 0 :(得分:8)

var results = from t1 in Table1
   from t2 in Table2
   where t1.ID = t2.AnotherID
   join t3 in Table3 on t1.ID equals t3.AnotherID into joined
   from j in joined.DefaultIfEmpty()
   select new {t1, t2, t3 = j}