日斋
日新月异

var q =
                (from ar in db.Articles
                 join c in db.Categories on ar.ParentId
                     equals c.Id
                 join sc in db.Categories on ar.SubId equals sc.Id into scate
                 from sc in scate.DefaultIfEmpty()
                 where ar.IsPrivate == false
                 orderby ar.CreateTime descending
                 __select new
                            {
                               article = ar,
                               pcate = c,
                               scate = sc
       
    }).Take(3);


defaultifempty()方法就可以实现左连接,并且左右连接都是外连接的一种.
var result = from s in 集合或表
 join t in 另一个集合或表 on s.id equals t.id into u
 from v in u.defaultifempty()
 select new { 需要的字段或值};

 

 

LinQ To Entities Left Join

 

(from u in originalDatabase.BS_Class select u)
.Join(originalDatabase.BS_ClassType, t1 => t1.sClassTypeCode, t2 => t2.sCode, (t1, t2) => new { t1, t2 })
.Join(originalDatabase.BS_Project, t12 => t12.t2.sProjectCode, t3 => t3.sCode, (t12, t3) => new { t12.t1, t12.t2, t3 })
.Join(originalDatabase.S_Dept, t123 => t123.t3.sDeptCode, t4 => t4.sCode, (t123, t4) => new { t123.t1,t123.t2,t123.t3,t4})

posted on 2011-08-25 16:39  李承隆  阅读(465)  评论(0编辑  收藏  举报