我无法对LINQ表达式进行以下查询。你可以帮帮我吗 ?
SELECT Employee_Id_FK from TimeRegistrations as tr
JOIN Employees em ON tr.Employee_Id_FK = em.id
JOIN Managers m ON em.Manager_Id_FK = m.id
WHERE m.id = 4
这是我到目前为止所做的:
var result = from t in DB.TimeRegistrations
join Employees in DB.TimeRegistrations on t.Employee_Id_FK equals Employees.id
join Managers in DB.Managers on ..... ;
// Display results.
foreach (var r in result)
{
Console.WriteLine(r);
}
答案 0 :(得分:0)
如果您的数据库有外键:
var result =from t1 in DB.TimeRegistrations
join t2 in t1.Employees
join t3 in t2.Managers
where t3.id == "4"
select t1
如果没有:
var result =from t1 in DB.TimeRegistrations
join t2 in DB.Employees on t1.Employee_Id_FK equals t2.id
join t3 in DB.Managers on t2.id equals t3.Manager_Id_FK
where t3.id == "4"
select t1
但更短的方式是:
var result = DB.Managers.Find(4).Employees.SelectMany(e=>e.TimeRegistrations)
或者不那么直接:
var result = DB.TimeRegistrations
.Where(t=>t.Employees.Any(e=>e.Managers.Any(m=>m.id == 4)))