c# – Linq-to-Entities Left JOIN

前端之家收集整理的这篇文章主要介绍了c# – Linq-to-Entities Left JOIN前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的查询
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)
原文链接:https://www.f2er.com/csharp/100313.html

猜你在找的C#相关文章