1
我有4個表格;Linq Lambda多個表格(4個表格)LEFT JOIN
TMain >> MainId (PK)
T1 >> T1_Id, MainId, X (PK and FK) X is decimal
T2 >> T2_Id, MainId, X (PK and FK) X is decimal
T3 >> T3_Id, MainId, X (PK and FK) X is decimal
這裏SQL輸出;
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
我如何寫LINQ LAMDA
var AbbA = MyContext.TMain
.GroupJoin(
MyContext.T1,
q1 => q1.TMainId,
q2 => q2.TMainId,
(x, y) => new { A = x, T1_A = y })
.SelectMany(
xy => xy.T1_A.DefaultIfEmpty(),
(x, y) => new { A = x.A, T1_A = y })
.GroupJoin(
MyContext.T2,
q1 => q1.A.TMainId,
q2 => q2.TMainId,
(x, y) => new { A = x, T2_A = y })
.SelectMany(
xy => xy.T2_A.DefaultIfEmpty(),
(x, y) => new { A = x.A, T2_A = y })
.GroupJoin(
MyContext.T3,
q1 => q1.A.A.TMainId,
q2 => q2.TMainId,
(x, y) => new { A = x, T3_A = y })
.SelectMany(
xy => xy.T3_A.DefaultIfEmpty(),
(x, y) => new { A = x.A, T3_A = y })
.Select(q => 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();
所以我想要訪問
我寫q.A.A.T1_A.X或q.A.A.A. T1字段或TMain領域in linq select
這是真的嗎?或者有最簡單的方法?
儘量參見[101個LINQ樣品:加入運營商(HTTP:// MSDN。 microsoft.com/ru-ru/vstudio/ee908647.aspx#leftouterjoin) – Grundy
嗨,感謝您的回覆我發現了一些答案,但不是LAMBDA http://stackoverflow.com/questions/16049586/linq-query-to-join -4-表 – AbbA