Linq 实现左连接,右连接(转http://gaojianzhuang110.blog.163.com/blog/static/186131462011410102529286/)

Coordinator
Nov 4, 2011 at 2:33 PM

 

2011-05-10 10:25:29|  分类: dotnet技术笔记 |  标签:employee  deptid  id  join  department   |字号  订阅

左连接:

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
};