Implementing a LEFT join with a inner join in LINQ to SQL using LINQ syntax with lambda expressions

Source: Internet
Author: User
Tags sql using

There are currently two tables, Sgroup and Sgroupuser, which are associated by Gkey, while the Sgroup table records the group, and the Sgroupuser record is the user in the group, so there is no data in the Sgroupuser. You need to use the left join to get the data:

The LINQ syntax is as follows:

var sg = ( from the dc.sgroup                     in  dc.sgroupuser on G.gkey equals Gu.gkey into L                     from in  l.defaultifempty ()                      SelectNew {g, LGU}). ToList ();

The lambda expression is as follows:

var New {g, gu}). Select (O=>o). ToList ();

Attention:

LINQ has a different result than a lambda expression. The number of records that LINQ takes out is the same as the result of a left join in SQL, and the number of records taken by a lambda expression is the number of records in the Sgroup table. Sgroupuser corresponding records are present in the result with the object collection

Report:

Here is the inner Join:

The LINQ syntax is as follows:

var sg = ( from the dc.sgroup                    in  dc.sgroupuser on G.gkey equals Gu.gkey                                      Selectnew {g, gu}). ToList ();

The lambda expression is as follows:

var New {g, gu}). Select (O=>o). ToList ();

Attention:

The ToList () method is used at the end, and ToList () is used to fetch the data locally at once.

Implementing a LEFT join with a inner join in LINQ to SQL using LINQ syntax with lambda expressions

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.