Linq-to-SQL转换为SQL语法

本文关键字:语法 SQL 转换 Linq-to-SQL | 更新日期: 2023-09-27 18:30:03

我正在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语法。

如何将左联接与内联接结合起来?

问候,

Linq-to-SQL转换为SQL语法

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}