LINQ 查询未返回所需的详细信息
LINQ query is not returning the required details
我有一个 table "employee" 其中有列 (id, emp_name, emp_email, mngr_email) 我还有另一个 table "mngr"
其中有列(id,电子邮件)。我想获取员工的员工记录和 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();
您的模型可以设计得更好,但根据您所拥有的,您需要第二次加入员工。您 link 通过电子邮件,所以:
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();
这是 employee/manager 自引用关系的更直接的方法:http://www.codeproject.com/Articles/206410/How-to-Configure-a-Self-Referencing-Entity-in-Code
我有一个 table "employee" 其中有列 (id, emp_name, emp_email, mngr_email) 我还有另一个 table "mngr" 其中有列(id,电子邮件)。我想获取员工的员工记录和 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();
您的模型可以设计得更好,但根据您所拥有的,您需要第二次加入员工。您 link 通过电子邮件,所以:
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();
这是 employee/manager 自引用关系的更直接的方法:http://www.codeproject.com/Articles/206410/How-to-Configure-a-Self-Referencing-Entity-in-Code