Lambda多表联合

var query = database.Posts    // your starting point - table in the "from" statement
   .Join(database.Post_Metas, // the source table of the inner join
      post => post.ID,        // Select the primary key (the first part of the "on" clause in an sql "join" statement)
      meta => meta.Post_ID,   // Select the foreign key (the second part of the "on" clause)
      (post, meta) => new { Post = post, Meta = meta }) // selection
   .Where(postAndMeta => postAndMeta.Post.ID == id);    // where statement

  var qry= dataContext.Parent
                   .GroupJoin(
                      dataContext.ChildItems,
                      dl => dl.LinkId,
                      itm => (Int32?)(itm.ChildItemId),
                      (x, y) =>
                         new
                         {
                             Parent = x,
                             ChildItems = y
                         }
                   )
                   .Where(x => (((x.Parent.ParentId == 1)) 
                       && (x.Parent.LinkTypeId == 1)
                   )
                   .SelectMany(
                      x => x.ChildItems.DefaultIfEmpty(),
                      (x,y) => new { Parent=x.Parent, ChildItems=y});

            var tmpResult = result.ToList();
 
posted on 2017-07-27 21:59  hahahoo  阅读(8093)  评论(0编辑  收藏  举报