LINQ到实体LEFT JOIN [英] Linq-to-Entities Left JOIN

查看:142
本文介绍了LINQ到实体LEFT JOIN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的查询:

 从论坛,论坛
的帖子在论坛上加入后等于post.Forum成由对在postGroup
postGroup

,其中p.ParentPostID == 0

选择新的
{
forum.Title,
forum.ForumID,
LastPostTitle = p.Title,
LastPostAddedDate = p.AddedDate
})排序依据(O =方式> o.ForumID)

目前的加入不剩加入,如果一些论坛不具备属于它的意思后,将不予退还。结果
没有帖子的论坛必须以空(或默认)值后的属性返回。



更新



结果集应该是这样的一些东西:

  ForumId | ForumTitle | LastPostTitle | LastPostAddedDate 
-------- + ------------ + --------------- + ------- -----------
4 |体育|布拉布拉| 2010年12月4日
4 |体育|布拉布拉| 15/4/2010
6 |游戏|布拉布拉| 2010年1月5日
7 |火焰| |


解决方案

你有没有尝试这样的:

 从论坛,从(Posts.Where(qPosts => forum.ForumId == qPosts.ForumId))职位论坛
。 DefaultIfEmpty()
,其中posts.ParentPostID == 0
排序依据forum.ForumId
选择新的
{
forum.Title,
forum.ForumID,
LastPostTitle = posts.Title,
LastPostAddedDate = posts.AddedDate
}


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.ParentPostID==0

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

Currently the Join is not left join, meaning if some forum doesn't have a post that belongs to it, it will not be returned.
The forum without posts must be returned with null (or default) values for the post properties.

UPDATE

The result set should be some thing like that:

ForumId | ForumTitle | LastPostTitle | LastPostAddedDate  
--------+------------+---------------+------------------
4       |   Sport    |    blabla     |       12/4/2010  
4       |   Sport    |    blabla     |       15/4/2010  
6       |   Games    |    blabla     |       1/5/2010  
7       |   Flame    |               |

解决方案

Did you try something like:

from forum in Forums
from posts in (Posts.Where(qPosts=> forum.ForumId == qPosts.ForumId)).DefaultIfEmpty()
where posts.ParentPostID == 0
orderby forum.ForumId 
select new
{
    forum.Title,
    forum.ForumID,
    LastPostTitle = posts.Title,
    LastPostAddedDate = posts.AddedDate
}

这篇关于LINQ到实体LEFT JOIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆