在Entity FrameWork中使用/ join到lambda表达式

时间:2011-06-14 12:33:44

标签: entity-framework lambda

您好我有一个SQL查询如下。

select * 
from Customers as C 
where
c.CompanyID in (
  select CompanyID 
  from Projects 
  where Projects.ID in (
    select ProjectAssignment.ProjectID 
    from ProjectAssignment 
    where ProjectAssignment.EmployeeID = 7 
  ) or Projects.ID in (
    select T.ProjectID
    from Tasks as T 
    where t.AssignedUserID = 7
  )
)

如何用lambda表达式编写它?

1 个答案:

答案 0 :(得分:1)

尝试(不确定它是否有效):

var projectIds1=ProjectAssignment.Where(pa=>pa.EmployeeID ==7).Select(ps=>pa.ProjectID);

var projectIds2=Tasks.Where(t=>t.AssignedUserID ==7).Select(t=>t.ProjectID);

var companyIds=Projects.Where(p=>projectIds1.Contains(p.ID) || projectIds2.Contains(p.ID)).
    Select(p=>p.CompanyID);

var result=Customers.Where(c=>companyIds.Contains(c.CompanyID));