加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Linq-to-Entities Left JOIN

发布时间:2020-12-15 08:00:52 所属栏目:百科 来源:网络整理
导读:这是我的查询: 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 }).
这是我的查询:
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)

目前,Join不是左连接,这意味着如果某个论坛没有属于它的帖子,则不会返回.
没有帖子的论坛必须返回post属性的null(或默认)值.

UPDATE

结果集应该是这样的:

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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读