我有一张桌子"员工"它有列(id,emp_name,emp_email,mngr_email),我还有另一个表" mngr" 其中包含列(id,email)。我想获得员工的员工记录和mngr记录。最终mngr也是一名员工,所以我希望mngr名称也在记录中。请帮我查询。
var rec = (from em in Context.employee
join mn in .Context.mngr on em.id equals mn.id
where em.id == 1
select new Records()
{
Name = em.name,
emp_email = em.email,
mngr_email = mn.email,
mngr_name = ---?,
mngr_id = mn.id
}).ToList();
答案 0 :(得分:1)
您的模型可以更好地设计,但根据您所需要的第二次加入员工。您通过电子邮件进行链接,所以:
var rec= (from em in Context.employee
left join me in Context.employee on me.email equals em.mngr_email
where em.id==1
select new Records()
{
Name = em.name,
emp_email= em.email,
mngr_email = em.mngr_email,
mngr_name = me.name,
mngr_id = me.id
}).ToList();
以下是员工/经理自我引用关系的更直接方法:http://www.codeproject.com/Articles/206410/How-to-Configure-a-Self-Referencing-Entity-in-Code