目的实现:select id,name,jname,cname from userinfo u left join job j on u.job=j.jid left join city c on u.city=c.cid 多表left join
var list = (from u in dc.userinfos
join j in dc.jobs on u.job equals j.jid into j_join
from x in j_join.DefaultIfEmpty()
join c in dc.cities on u.city equals c.cid into c_join
from v in c_join.DefaultIfEmpty()
select new
{
id=u.id,
name=u.name,
jname=x.jname,
cname=v.cname,
/*u1=u,x1=x,v1=v*///不要用对象的方式 因为对象可能为null那么对象.属性就会抛异常
}).ToList();
for (var i = 0; i < list.Count(); i++)
{
Console.WriteLine(list[i].name+'\t'+list[i].jname+'\t'+list[i].cname); //字段为null不跑异常
//Console.WriteLine(list[i].u1.name+'\t'+list[i].x1.jname+'\t'+list[i].v1.cname+"\r\n"); //对象x1 v1 有可能为null 抛异常
}
Console.ReadLine();