How to perform left join in LINQ to SQL

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.