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

LINQ多表连接
C# code

                var query = from g in db.graveTemp
                            join a in db.Area
                            on g.fgrid equals a.Id into temp
                            from t in temp.DefaultIfEmpty()
                            join s in db.SubArea
                            on g.subarea equals s.Id into temp1
                            from t1 in temp1.DefaultIfEmpty()
                            join c in db.Customer
                            on g.gvid equals c.FgrvId into temp3
                            from t3 in temp3.DefaultIfEmpty()
                            select new 
                            {
                                gvid=g.gvid,
                                gno=g.gno,
                                gname=g.gname,
                                areaid=g.fgrid,
                                areaName=t==null?null:t.AreaName,   //地区名
                                kindId=t==null?null:t.FgkID,        //类型编号
                                subAreaId=g.subarea,
                                subAreaName=t1==null?null:t1.SubName,
                                boxes=g.grvboxes,
                                state=t3==null?"否":"是"
                            };


这么写貌似有点不规范 subareaname处 数据有误 看着凌乱的给个3表左链接的示例就行 3q

------解决方案--------------------
http://topic.csdn.net/u/20110407/10/55a95e14-0f2e-4faa-b5e5-4d12bdeed169.html
------解决方案--------------------
5楼的 或许我这样写 你容易理解一点:

var query = from g in db.graveTemp
join a in db.Area
on g.fgrid equals a.Id into temp
from a in temp.DefaultIfEmpty()
select new{ gname=g.gname,areaName=a==null?"":a.AreaName //地区名}