日期:2014-05-20  浏览次数:20880 次

linq 三表联合查询,在线等高人指点。。
现在有三张表
表A 、B、C
a b c
aid bid cid
bid aid enid
enid work work

要实现的效果 b.work c.work
其中c.work 只有一条数据
b.work有三条数据

怎么样实现查询出来b有三个值,c只有一个值

求高人帮忙!!!

------解决方案--------------------
其实是要Left Join一下,我那个写错了

C# code
var works = 
           from a in A join b in B on a.aid equals b.aid into AB select new{a.enid, b.work}
           from ab in AB.DefaultIfEmpty() join c in C on c.enid equals ab.enid
           select new{workB = b.work, 
                      workC = c.work //?? 默认值 
           };

------解决方案--------------------
C# code

var query= from p in A
           join q in B
           on p.aid equals q.aid
           into temp
           from r in temp.DefaultIfEmpty()
           join s in C
           on q.enid equals s.enid
           into temp1
           from s in temp1.DefaultIfEmpty()
           select new 
           {
              q.work,
              r.work
           };