INNER JOIN 일치하는 요소만 결과 집합에 포함됩니다. 일치하지 않는 요소는 결과 집합에서 제외됩니다.
LEFT OUTER JOIN 사용 일치하는 모든 요소 + 왼쪽 컬렉션의 일치하지 않는 모든 요소가 결과 집합에 포함됩니다.
예를 들어 왼쪽 외부 조인 구현을 이해합시다. 다음 Department 및 Employee 클래스를 고려하십시오. Mary 직원에게는 할당된 부서가 없습니다. 내부 조인은 결과 집합에 그녀의 레코드를 포함하지 않지만 왼쪽 외부 조인은 포함합니다.
예시
static class Program{
static void Main(string[] args){
var result = Employee.GetAllEmployees()
.GroupJoin(Department.GetAllDepartments(),
e => e.DepartmentID,
d => d.ID,
(emp, depts) => new { emp, depts })
.SelectMany(z => z.depts.DefaultIfEmpty(),
(a, b) => new{
EmployeeName = a.emp.Name,
DepartmentName = b == null ? "No Department" : b.Name
});
foreach (var v in result){
Console.WriteLine(" " + v.EmployeeName + "\t" + v.DepartmentName);
}
}
}
public class Department{
public int ID { get; set; }
public string Name { get; set; }
public static List<Department> GetAllDepartments(){
return new List<Department>(){
new Department { ID = 1, Name = "IT"},
new Department { ID = 2, Name = "HR"},
};
}
}
public class Employee{
public int ID { get; set; }
public string Name { get; set; }
public int DepartmentID { get; set; }
public static List<Employee> GetAllEmployees(){
return new List<Employee>(){
new Employee { ID = 1, Name = "Mark", DepartmentID = 1 },
new Employee { ID = 2, Name = "Steve", DepartmentID = 2 },
new Employee { ID = 3, Name = "Ben", DepartmentID = 1 },
new Employee { ID = 4, Name = "Philip", DepartmentID = 1 },
new Employee { ID = 5, Name = "Mary" }
};
}
}