2012-05-25 37 views
1

我有两个表:linq to sql。两次相对表

tab1              tab2 

ID | Name | Sername | PostID     ID | PostDecription 

的问题:我怎么能在细胞细胞帖子ID TAB1显示从TAB2 PostDecription如果PostDecription可能有NULL值?

(from p in tab1 join s in tab2 on p.PostID equals 
             s.ID select new 
        {     
         ID = p.ID, 
         Name= p.Name, 
         Sername = p.Sername, 
         PostID = s.PostDecription, 

        }) 

使用此代码我只能得到在两个表中具有相同值的单元格。什么时候PostDecription可能具有值“NULL”的情况?

回答

2

你需要一个左连接

from p in tab1 
join s in tab2 on p.PostID equals s.ID into tab2s 
from s in tab2s.DefaultIfEmpty()  
select new 
        {     
         ID = p.ID, 
         Name= p.Name, 
         Sername = p.Sername, 
         PostID = s.PostDecription, 

        }