标签:style blog color ar strong sp div art c
左连接:
var LeftJoin = from emp in ListOfEmployees
join dept in ListOfDepartment
on emp.DeptID equals dept.ID into JoinedEmpDept
from dept in JoinedEmpDept.DefaultIfEmpty()
select new
{
EmployeeName = emp.Name,
DepartmentName = dept != null ? dept.Name : null
};
右连接:
var RightJoin = from dept in ListOfDepartment
join employee in ListOfEmployees
on dept.ID equals employee.DeptID into joinDeptEmp
from employee in joinDeptEmp.DefaultIfEmpty()
select new
{
EmployeeName = employee != null ? employee.Name : null,
DepartmentName = dept.Name
};
看到网上如是的解左联和右联,我可以总结为:如果需要左表所有记录,就用左连,如果需要右表所有记录,就用右联,这是在Sql中的方法,在Linq中没有右联,只有变相思考,右联就是把右边的表放到前面,然后再进行左联,就达到了右联的目的。
标签:style blog color ar strong sp div art c
原文地址:http://www.cnblogs.com/chengxuyuandadao/p/4007039.html