如何在LINQ中执行多个连接

时间:2013-06-27 10:04:59

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

我有一个存储过程,我正在转换为LINQ,但我有点卡住了。这是SQL:

    select 
      @name                             = tp.Name
,     @Entity                           = tc.Entity
,     @Name                             = tc.Name
,     @ID                               =  tpt.ID
,     @Code                             =  tptr.Code  

    from tbltprop tp 

    Left join tblcol tc                                on ( tc.id = tp.ID )
    Left join tblPropday  tpt               on ( tpt.Id = tp.Id )
    Left join tblProperResult tptr   on (tptr.ID = tpt.Id )

    where tp.id               = @chsarpID1 //input ID i get in c#
    and     tpt.Id = @chsarpID2

我理解如何进行单个连接,但是我在进行多个连接工作时遇到了一些麻烦。怎么会这样呢?

1 个答案:

答案 0 :(得分:2)

这可能对你有帮助.......

var values= from tp in tbltprop
            join tb in tblcol ON tp.id equals tb.id into gj
            from gjd in gj.DefaultIfEmpty()
            join tpt in tblPropday ON tp.id equals tpt.id into gk
            from gkd in gk.DefaultIfEmpty()
            join tptr in tblProperResult ON tp.id equals tptr.id into gl
            from gld in gl.DefaultIfEmpty()
            where tp.id == @charpID1 && gkd.Id == @CharpID2
            select new 
                   { 
                       TpName = tp.Name, 
                       Entity = gjd.Entity, 
                       TPTName = gjd.Name, 
                       ID = gkd.ID, 
                       Code = gld.Code
                   };