LINQ to SQL Series IV using inner Join,outer join

Source: Internet
Author: User

Let's look at one of the simplest inner joins, inner the corresponding class information from the Join Class table when reading the student table:

Static voidMain (string[] args) {    using(varwriter =NewStreamWriter (Watchsqlpath,false, Encoding.UTF8)) {        using(Dbappdatacontext db =NewDbappdatacontext ()) {db. Log=writer; //INNER JOIN            varquery = fromSinchdb. Students Join Cinchdb. Classes on S.classid equals C.classidwhereC.classid = =1                        Select New{ClassID=S.classid, ClassName=C.classname, Student=New{Name=S.name, ID=S.studentid}}; foreach(varIteminchquery) {Console.WriteLine ("{0} {1} {2}", item. ClassID, item. ClassName, item.            Student.name); }}} console.readline ();}

Especially simple, do not post SQL, continue to see outer join:

Static voidMain (string[] args) {    using(varwriter =NewStreamWriter (Watchsqlpath,false, Encoding.UTF8)) {        using(Dbappdatacontext db =NewDbappdatacontext ()) {db. Log=writer; //Left outer join            varquery = fromSinchdb. Students Join Cinchdb. Classes on S.classid equals C.classid into GC fromGciinchGC. DefaultIfEmpty ()Select New{ClassID=S.classid, ClassName=GCI. ClassName, Student=New{Name=S.name, ID=S.studentid}}; foreach(varIteminchquery) {Console.WriteLine ("{0} {1} {2}", item. ClassID, item. ClassName, item.            Student.name); }}} console.readline ();}

Outer join must have the join table into a new variable GC and then use the GC. DefaultIfEmpty () represents an outer join.

LINQ to SQL Related essays:

1. Starting with cud, how to insert, modify, and delete data using LINQ to SQL

2. Query using LINQ to SQL for simple queries

3. Query lazy load with immediate load, using LoadWith and AssociateWith

4. Query Inner join,left outer JOIN

5. Aggregation grouping in Linq to SQL have

6. LINQ to SQL query optimization, do you need to worry about performance?

LINQ to SQL Series IV using inner Join,outer join

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.