LINQ Learning (7): Join clause

Source: Internet
Author: User

Note: In relational databases, join can be used for operations on multiple tables.Inner join, outer join, and cross join. Similarly, in the LINQ query syntax, join clauses are also very important in Multi-Table operations,UseJoinClauses can be used to associate elements from different source sequences without any direct relationship in the object model.JoinClause to execute equivalent join, useEqualsKeyword instead=Operator.

 

Join clauseThe operation includes three forms:Internal join, Group join, left external join.The following example uses an instance:

Test data:

         ///   <Summary>          ///  Student table  ///   </Summary>          Class Student {  Public   String Id { Get ; Set  ;}  Public   String Name { Get ; Set  ;}}  ///   <Summary>          ///  Course schedule ///   </Summary>          Class  Course {  Public   String Courseid { Get ; Set  ;}  Public   String Coursename { Get ; Set  ;} List <Student> students =New List <student> {  New Student () {id = "  C03  " , Name = "  Terry  "  },  New Student () {id = "  C03  " , Name = " James  "  },  New Student () {id = "  C01  " , Name = "  Kobe  "  },  New Student () {id = "  C02  " , Name = " AI  "  },  New Student () {id = "  C01  " , Name = "  Wade  "  },  New Student () {id = "  C05  " , Name = " Kelly  "  },}; List <Course> Cours = New List <course> {  New Course () {courseid = "  C01  " , Coursename = "  C # Course Design  "  },  New Course () {courseid ="  C02  " , Coursename = "  Java in-depth  "  },  New Course () {courseid = "  C03  " , Coursename = "  PHP application development  "  },  New Course () {courseid = "  C04  " , Coursename = "  IOS case description  "  }}; 

 

1. Internal Connection

Note :"An internal join produces a result set. For each element in the first set in the result set, this element appears once if a matching element exists in the second set. If an element in the first set has no matching element, it does not appear in the result set.

Query the course selection information of students:

             VaR Query =From Student In  Students join cour  In  Cours on student. ID equals cour. courseid  Select   New  {ID = Student. ID, name = Student. Name, courname = Cour. coursename }; Foreach ( VaR Student In  Query) {console. writeline (  "  {0} {1} {2}  "  , Student. ID, student. Name, student. courname );  //  C03 Terry PHP application development  //  C03 James PHP application development  //  C01 Kobe C # Course Design //  In-depth research on C02 AI Java  //  C01 Wade C # Course Design  //  Note: c05 does not match, so it will not appear in the result set. }

 

2. Group join

Note:ContainIntoExpressionJoinA clause is called a group join. Group joinIt is essentially an object array sequence, and the result sequence is organized into multiple groups of form data for return.A layered sequence of results is generated. In general, each element in the first set of the sequence is paired with a group of related elements in the second set. If no elements are found, an empty array is returned. In my understanding, Group join is similar to internal join.

Query the course selection information of students:

             VaR Query = From Student In Students join cour  In  Cours on student. ID equals cour. courseid into coursegroup  Select   New  {ID = Student. ID, name = Student. Name, courinfo = Coursegroup };  Foreach ( VaR StudentIn  Query) {console. Write (  "  {0} {1}  "  , Student. ID, student. Name );  Foreach ( VaR Cour In  Student. courinfo) {console. Write (  "  {0}  " , Cour. coursename);} console. writeline ();  //  C03 Terry PHP application development  //  C03 James PHP application development  //  C01 Kobe C # Course Design  //  In-depth research on C02 AI Java  //  C01 Wade C # Course Design  //  C05 Kelly  // Note: c05 does not match, but an empty array is returned. }

 

3. left Outer Join

Note: In the left Outer Join, all elements in the left source sequence are returned, even if they do not match in the right sequence . defaultifempty on the results of a group join. "> you can use LINQ to call defaultifempty for the Group join result to execute the left Outer Join.

Query the course selection information of students:

             VaR Query = From Student In  Students join cour  In Cours on student. ID equals cour. courseid into coursegroup  From Stucour In  Coursegroup. defaultifempty ()  Select   New  {ID = Student. ID, name = Student. Name, Cour = Coursegroup };  Foreach (VaR Student In  Query) {console. Write (  "  {0} {1}  "  , Student. ID, student. Name );  Foreach ( VaR Cour In  Student. Cour) {console. Write (  "  {0}  " , Cour. coursename);} console. writeline ();  //  C03 terryc03 PHP application development  //  C03 jamesc03 PHP application development  //  C01 kobec01 C # Course Design  //  In-depth research on C02 aic02 Java  //  C01 wadec01 C # Course Design  //  C05 Kelly
// Note: all elements in the left-side source sequence are returned. }

 

 

Author: forevernome
Source: http://www.cnblogs.com/ForEvErNoME/
You are welcome to repost or share it, but be sure to declare it Article Source. If the article is helpful to you, I hope you can Recommendation Or Follow

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.