Yo-ho-ho
Yo-ho-ho

Reputation: 117

linq to sql. two relative tables

I have two tables:

tab1                                                     tab2

ID | Name | Sername | PostID                  ID | PostDecription

The question: how can I show in tab1 in cell PostID cell from tab2 PostDecription if PostDecription could has value 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,

                    })

Using this code I can get only cells that have the same value in two tables. What about case when PostDecription could has value "NULL"???

Upvotes: 1

Views: 102

Answers (1)

Adrian Iftode
Adrian Iftode

Reputation: 15673

You need a left join

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,

                    }

Upvotes: 2

Related Questions