这是我的查询:
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不是左连接,这意味着如果某个论坛没有帖子属于它,它将不会被返回。
没有帖子的论坛必须返回null(或默认)值作为帖子属性。
更新
结果集应该是这样的:
ForumId | ForumTitle | LastPostTitle | LastPostAddedDate
--------+------------+---------------+------------------
4 | Sport | blabla | 12/4/2010
4 | Sport | blabla | 15/4/2010
6 | Games | blabla | 1/5/2010
7 | Flame | |
from p in postGroup
修改为from p in postGroup.DefaultIfEmpty()
。 - Jens Kloster