Linq-to-Entities Left JOIN

前端 未结 7 776
终归单人心
终归单人心 2021-01-12 09:02

This is my query:

from forum in Forums
    join post in Posts on forum equals post.Forum into postGroup    

    from p in postGroup     
    where p.ParentP         


        
相关标签:
7条回答
  • 2021-01-12 09:29
    Forums
        .GroupJoin(PostGroup, f => f.ID, p => p.ForumID, (f, p) => new { Forum = f, PostList = p })
        .Where(anon => anon.PostList.Any(pl => pl.ParentPostID.Equals(0)))
        .OrderBy(anon => anon.Forum.ForumID)
        .Select(anon => new
        {
            Title = anon.Forum.Title,
            ForumID = anon.Forum.ForumID,
            LastPostTitle = anon.PostList.FirstOrDefault().Title,
            LastPostAddedDate = anon.PostList.FirstOrDefault().AddedDate,
        });
    

    Something similar to this. I wasn't too sure because I didn't really have a view of the data model, but GroupJoin should be very similar to LEFT OUTER JOIN even though it doesn't realistically produce that in SQL.

    0 讨论(0)
提交回复
热议问题