LINQ INNER JOIN有2个内连接

时间:2011-12-05 15:23:18

标签: linq

如何在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

                 });

1 个答案:

答案 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异常,我不会感到惊讶。 请告诉我们。