简体   繁体   English

SQL查询到LINQ表达式

[英]SQL query to LINQ expression

I cannot make the following query into LINQ expression. 我无法对LINQ表达式进行以下查询。 Could you help me ? 你可以帮帮我吗 ?

 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 

This is what I have so far : 这是我到目前为止所拥有的:

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);
}

In case your db has foreign keys: 如果您的数据库具有外键:

 var result =from t1 in DB.TimeRegistrations
     join t2 in t1.Employees 
     join t3 in t2.Managers 
     where t3.id == "4"
     select t1

in case it does not: 如果没有:

 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

However shorter way will be: 但是,更简单的方法是:

 var result = DB.Managers.Find(4).Employees.SelectMany(e=>e.TimeRegistrations)

Or not that straight forward way: 还是不是那种简单的方法:

 var result = DB.TimeRegistrations
                .Where(t=>t.Employees.Any(e=>e.Managers.Any(m=>m.id == 4)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM