You can use the into syntax and the defaultifempty () method to load null values in the right table.
Example:
VaR query =
From R in db. Roles
Join Sr in
(
From SS in db. staffinroles
Where ss. staffid = staffid
Select SS
)
On R. ID equals Sr. roleid
Into X
From y in X. defaultifempty ()
Select New roleinfo
{
Roleid = R. ID,
Rolename = R. Name,
Isset = y! = NULL
};
Note that you can use y at the end! = NULL to determine whether the right value is null.
The corresponding SQL generated by this LINQ to SQL statement is:
Exec sp_executesql n' select [t0]. [ID] as [roleid], [t0]. [name] as [rolename],
(Case
When [T2]. [test] is not null then 1
Else 0
End) as [isset]
From [role] as [t0]
Left Outer Join (
Select 1 as [test], [T1]. [staffid], [T1]. [roleid]
From [staffinrole] as [T1]
) As [T2] On ([t0]. [ID] = [T2]. [roleid]) and ([T2]. [staffid] = @ P0) ', n' @ P0 int', @ p0 =1