组织结构图不显示 ID 为 null 的经理详细信息 --- 使用左外连接的自连接
Organization chart does not show Manager Details with id null --- self join with left outer join
我正在做一个组织结构图练习,显示列表中的组织,然后添加、删除方法。
我正在使用带左外连接的 LINQ 自连接来显示经理员工详细信息,但我无法显示经理 ID 为空的 CEO。
// This block throws null refrence exception saying t1 is null
//Managername is null
var emp = (from m in employee
join e1 in employee on m.ManagerId equals e1.EmpId into temp
from t1 in temp.DefaultIfEmpty()
select new Employee
{
EmpId = m.EmpId,
EmployeeName = m.EmployeeName,
Gender = m.Gender,
ManagerId = t1?.ManagerId ?? 0 ,
ManagerName = t1.EmployeeName,
Designation = m.Designation
}).ToList();
public class Employee
{
public int EmpId { get; set; }
public string EmployeeName { get; set; }
public char Gender { get; set; }
public int? ManagerId { get; set; }
public string Designation { get; set; }
public string ManagerName { get; set; }
}
List<Employee> employee = new List<Employee>();
employee.Add(new Employee { EmpId = 3, EmployeeName = "Andrea Ely", Gender = 'F', ManagerId = 6, Designation = "Tech Head"});
employee.Add(new Employee { EmpId = 4, EmployeeName = "John Eward", Gender = 'M', ManagerId = 3, Designation = "General" });
employee.Add(new Employee { EmpId = 5, EmployeeName = "Lesli Mac", Gender = 'F', ManagerId = 3, Designation = "Staff" });
employee.Add(new Employee { EmpId = 6, EmployeeName = "Satinder Singh", Gender = 'M', ManagerId = 13, Designation = "President" });
employee.Add(new Employee { EmpId = 7, EmployeeName = "Amit Sarna", Gender = 'M', ManagerId = 13, Designation = "Vice President" });
employee.Add(new Employee { EmpId = 8, EmployeeName = "David Steward", Gender = 'M', ManagerId = 7, Designation = "General" });
employee.Add(new Employee { EmpId = 9, EmployeeName = "Rogger Binny", Gender = 'M', ManagerId = 7, Designation = "Sales Executive" });
employee.Add(new Employee { EmpId = 10, EmployeeName = "Emy jackson", Gender = 'F', ManagerId = 7, Designation = "Sales Executive" });
employee.Add(new Employee { EmpId = 11, EmployeeName = "Chris Harris", Gender = 'M', ManagerId = 7, Designation = "Executive" });
employee.Add(new Employee { EmpId = 13, EmployeeName = "Rocky Balbo", Gender = 'M', ManagerId = null, Designation = "CEO" });
// This block throws null refrence exception saying t1 is null
//Managername is null
var emp = (from m in employee
join e1 in employee on m.ManagerId equals e1.EmpId into temp
from t1 in temp.DefaultIfEmpty()
select new Employee
{
EmpId = m.EmpId,
EmployeeName = m.EmployeeName,
Gender = m.Gender,
ManagerId = t1?.ManagerId ?? 0 ,
ManagerName = t1.EmployeeName,
Designation = m.Designation
}).ToList();
Console.WriteLine("--------------Printing Manager-Employee Hierarchy-------------");
foreach (var empDetails in emp)
{
Console.WriteLine("Emp Name is {0} ManagerName is {1}", empDetails.EmployeeName, empDetails.ManagerName);
}
您可以使用左连接,如下所示:
var emp = (from m in employee
join e1 in employee on m.ManagerId equals e1.EmpId into temp
from t1 in temp.DefaultIfEmpty()
select new Employee
{
EmpId = m.EmpId,
EmployeeName = m.EmployeeName,
Gender = m.Gender,
ManagerId = t1?.ManagerId ?? 0 ,
ManagerName = t1?.EmployeeName,
Designation = m.Designation
}).ToList();
我正在做一个组织结构图练习,显示列表中的组织,然后添加、删除方法。
我正在使用带左外连接的 LINQ 自连接来显示经理员工详细信息,但我无法显示经理 ID 为空的 CEO。
// This block throws null refrence exception saying t1 is null
//Managername is null
var emp = (from m in employee
join e1 in employee on m.ManagerId equals e1.EmpId into temp
from t1 in temp.DefaultIfEmpty()
select new Employee
{
EmpId = m.EmpId,
EmployeeName = m.EmployeeName,
Gender = m.Gender,
ManagerId = t1?.ManagerId ?? 0 ,
ManagerName = t1.EmployeeName,
Designation = m.Designation
}).ToList();
public class Employee
{
public int EmpId { get; set; }
public string EmployeeName { get; set; }
public char Gender { get; set; }
public int? ManagerId { get; set; }
public string Designation { get; set; }
public string ManagerName { get; set; }
}
List<Employee> employee = new List<Employee>();
employee.Add(new Employee { EmpId = 3, EmployeeName = "Andrea Ely", Gender = 'F', ManagerId = 6, Designation = "Tech Head"});
employee.Add(new Employee { EmpId = 4, EmployeeName = "John Eward", Gender = 'M', ManagerId = 3, Designation = "General" });
employee.Add(new Employee { EmpId = 5, EmployeeName = "Lesli Mac", Gender = 'F', ManagerId = 3, Designation = "Staff" });
employee.Add(new Employee { EmpId = 6, EmployeeName = "Satinder Singh", Gender = 'M', ManagerId = 13, Designation = "President" });
employee.Add(new Employee { EmpId = 7, EmployeeName = "Amit Sarna", Gender = 'M', ManagerId = 13, Designation = "Vice President" });
employee.Add(new Employee { EmpId = 8, EmployeeName = "David Steward", Gender = 'M', ManagerId = 7, Designation = "General" });
employee.Add(new Employee { EmpId = 9, EmployeeName = "Rogger Binny", Gender = 'M', ManagerId = 7, Designation = "Sales Executive" });
employee.Add(new Employee { EmpId = 10, EmployeeName = "Emy jackson", Gender = 'F', ManagerId = 7, Designation = "Sales Executive" });
employee.Add(new Employee { EmpId = 11, EmployeeName = "Chris Harris", Gender = 'M', ManagerId = 7, Designation = "Executive" });
employee.Add(new Employee { EmpId = 13, EmployeeName = "Rocky Balbo", Gender = 'M', ManagerId = null, Designation = "CEO" });
// This block throws null refrence exception saying t1 is null
//Managername is null
var emp = (from m in employee
join e1 in employee on m.ManagerId equals e1.EmpId into temp
from t1 in temp.DefaultIfEmpty()
select new Employee
{
EmpId = m.EmpId,
EmployeeName = m.EmployeeName,
Gender = m.Gender,
ManagerId = t1?.ManagerId ?? 0 ,
ManagerName = t1.EmployeeName,
Designation = m.Designation
}).ToList();
Console.WriteLine("--------------Printing Manager-Employee Hierarchy-------------");
foreach (var empDetails in emp)
{
Console.WriteLine("Emp Name is {0} ManagerName is {1}", empDetails.EmployeeName, empDetails.ManagerName);
}
您可以使用左连接,如下所示:
var emp = (from m in employee
join e1 in employee on m.ManagerId equals e1.EmpId into temp
from t1 in temp.DefaultIfEmpty()
select new Employee
{
EmpId = m.EmpId,
EmployeeName = m.EmployeeName,
Gender = m.Gender,
ManagerId = t1?.ManagerId ?? 0 ,
ManagerName = t1?.EmployeeName,
Designation = m.Designation
}).ToList();