Linq-to-Entities Left JOIN

前端 未结 7 779
终归单人心
终归单人心 2021-01-12 09:02

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.ParentP         


        
7条回答
  •  迷失自我
    2021-01-12 09:04

    Try something like this:

    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 = postGroup.FirstOrDefault(p => p.ParentPostID==0).Title, 
        LastPostAddedDate = (DateTime?)postGroup.FirstOrDefault(p => p.ParentPostID==0).AddedDate          
    }).OrderBy(o=>o.ForumID)
    

    properties that return empty from the left join must also be nullable. So int => int? and DateTime => DateTime? etc..

提交回复
热议问题