2012-05-25 40 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, 

        }