如何在LINQ中进行以下操作:
SELECT T.fn, T.ln, T.Phone
FROM PG P
LEFT JOIN BUD B ON P.PgID = B.PgID
LEFT JOIN TERR T ON B.Tir= T.Tir
我尝试了以下但没有奏效:
var result = (from pr in db.PG
join b in db.BUD on pr.PgID equals b.PgID into tl_b
join tr in db.TERR on b.Tir equals tr.Tirr into tl_tr
from b in tl_b.DefaultIfEmpty()
from tr in tl_tr.DefaultIfEmpty()
select new {
tr.fn, tr.ln,tr.Phone
});
答案 0 :(得分:0)
删除into语句,不要重复使用名称(b,tr)
var result = (from pr in db.PG
join b in db.BUD on pr.PgID equals b.PgID
join tr in db.TERR on b.Tir equals tr.Tirr
from b2 in b.DefaultIfEmpty()
from tr2 in tr.DefaultIfEmpty()
select new {
tr2.fn,
tr2.ln,
tr2.Phone
});
尝试使用此功能,但如果您收到NullReference异常,我不会感到惊讶。 请告诉我们。