我有两张桌子User
& Employee
。
+-------Supervisor----------+
SupervisorId
Password
+---------------------+
+-------Employee----------+
EmployeeId
EmployeeSupervisorId
EmployeeName
+---------------------+
这是我到目前为止所做的事情
SupervisorName = db.Employee.Where(m => m.EmployeeSupervisorId == m.SupervisorId).Select(q => q.EmployeeName).ToList()
我不理解我如何将Employee表连接到自身的概念,以便我可以获得Employee列表及其相应的主管名称
答案 0 :(得分:0)
您可以执行以下操作
<CABasicAnimation: 0x7fbe13096b10>
heading layer animation did stop
<CABasicAnimation: 0x7fbe13020bf0>
heading layer animation did stop
<CABasicAnimation: 0x7fbe13096c80>
heading layer animation did stop
答案 1 :(得分:0)
SupervisorName = db.Employee.
Join(db.Supervisoer, e => e.EmployeeSupervisorId, s => s.SupervisorId, (e, s) => new { Employee = e, Supervisor = s}.
ToList().
Select(e => e.EmployeeName).
ToList();
答案 2 :(得分:0)
您可以使用像这样的简单子查询
var result = db.Employee.Select(e => new
{
Employee = e,
SupervisorName = db.Employee
.Where(s => s.EmployeeId == e.EmployeeSupervisorId)
.Select(s => s.EmployeeName).FirstOrDefault()
}).ToList();
答案 3 :(得分:0)
请注意,如果您将EmployeeSupervisorId定义为指向EmployeeId的外键,Linq2Sql将自动创建一个EmployeeSupervisor属性(这将是一个Employee对象)
var list = from e in db.Employee
// where e.......
select new {
Name = e.EmployeeName,
Supervisor = e.EmployeeSupervisor.EmployeeName,
// otherdetails = e......
}
如果尚未定义外键,则必须在查询中明确指定:
var list = from e in db.Employee
join s in db.Employee on e.EmployeeSupervisorId equal s.EmployeeId
// where e.......
select new {
Name = e.EmployeeName,
Supervisor = s.EmployeeName,
// otherdetails = e......
}