Linq中的left join

left join

            var custs = from c in db.T_Customer
                        join u in db.Sys_User on c.OwnerId equals u.Id into temp
                        from t in temp.DefaultIfEmpty()  //left join
                        where (c.PhoneNum1 == phone || c.PhoneNum2 == phone || c.PhoneNum3 == phone)
                        select new
                        {
                            CustName = string.IsNullOrEmpty(c.CustName) ? "匿名" : c.CustName,
                            UserName = t == null ? "暂无" : t.Name//这里主要第二个集合有可能为空。需要判断
                        };

 

posted @ 2017-03-23 16:10  自由的鱼  阅读(4055)  评论(0编辑  收藏  举报