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