尽管这看起来很基础,但是我在理解这个概念时仍在挣扎,左联接实际上是如何工作的?当没有分组的机会时,成关键字的目的是什么意思,然后为什么使用它来实现左联接??我们在这里如何分组?
public class LinqJoinController : Controller
{
//
// GET: /LinqJoin/
public List<Department> getAllDepartment()
{
return new List<Department>(){
new Department{Id=1,Name="IT"},
new Department{Id=2,Name="HR"},
};
}
public List<Employee> getAllEmployee()
{
return new List<Employee>(){
new Employee{Id=1,Name="Sachin",DepartmentId=1},
new Employee{Id=2,Name="Ram",DepartmentId=1},
new Employee{Id=3,Name="Shyam",DepartmentId=2},
new Employee{Id=4,Name="Radhe"},
};
}
public ActionResult Index()
{
var result = from emp in getAllEmployee()
join dep in getAllDepartment()
on emp.DepartmentId equals dep.Id
into eGroup
from dep in eGroup.DefaultIfEmpty()
select new EmployeeVM
{
DepartmentName = dep==null?"No Dept":dep.Name,
EmployeeName = emp.Name
};
return View(result);
}
}
}