gpt4 book ai didi

c# - Linq-to-Entity Left JOIN

转载 作者:太空狗 更新时间:2023-10-29 20:25:53 24 4
gpt4 key购买 nike

这是我的查询:

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

from p in postGroup
where p.ParentPostID==0

select new
{
forum.Title,
forum.ForumID,
LastPostTitle = p.Title,
LastPostAddedDate = p.AddedDate
}).OrderBy(o=>o.ForumID)

目前加入不是左加入,意思是如果某个论坛没有属于它的帖子,它不会被退回。
没有帖子的论坛的帖子属性必须返回空(或默认)值。

更新

结果集应该是这样的:
ForumId | ForumTitle | LastPostTitle | LastPostAddedDate  
--------+------------+---------------+------------------
4 | Sport | blabla | 12/4/2010
4 | Sport | blabla | 15/4/2010
6 | Games | blabla | 1/5/2010
7 | Flame | |

最佳答案

 var allforums = from f in context.Fora.Include("Posts")
select f;

此查询产生与以下相同的结果
            var allForums = from f in context.Fora  
select new ForumPosts
{
Forum = f,
Posts = context.Posts.Where(x=> x.ForumId == f.ForumId)

关于c# - Linq-to-Entity Left JOIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2772017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com