0

如何将此 sql 语句转换为 LINQ to Entities?

SQL 语句:

Select * from Departments where DepartmentID  in (Select DepartmentID from Employees where FirstName like '%FirstName%' or LastName like '%LastName%')

我所拥有的只是 WHERE =(equals) ,我不能执行 WHERE IN 子句

LINQ 到实体:

from t in db.Departments
where
  t.DepartmentID == -->Should be IN not EQUALS
    ((from t0 in db.Employees
    where
      t0.FirstName.Contains("FirstName") ||
      t0.LastName.Contains("LastName")
    select new {
      t0.DepartmentID
    }).FirstOrDefault().DepartmentID)
select t
4

2 回答 2

0
(from d in db.Departments
 join e in db.Employees
     on d.DepartmentID equals e.DepartmentID
 where e.FirstName.Contains("FirstName") ||
       e.LastName.Contains("LastName")
 select t).Distinct()
于 2013-05-15T02:37:41.900 回答
0

你可以用这个

var x = 
    (from t0 in db.Employees
    where
      t0.FirstName.Contains("FirstName") ||
      t0.LastName.Contains("LastName")
    select new {
      t0.DepartmentID
    }).FirstOrDefault().DepartmentID;

from t in db.Departments
where
  t.DepartmentID.contains(x.DepartmentID)
select t
于 2013-05-15T02:52:46.740 回答