2015-12-03 205 views
1

這是我簡單的查詢轉換SQL語句到LINQ

Select id from employee where id in 
(select employeeid from employeecenter where CenterCodeId in 
(select CenterCodeId from employeecenter where employeeid=40)) 

我們怎樣才能acheive上述使用LINQ

//Gets all the centerCodeIds allotted to an employee 
Session["LoggedUserId"] = 40; 
List<int> _centerCodeIds = _cmn.GetCenterEmpwise(Convert.ToInt32(Session["LoggedUserId"])) 
          .Select(x => x.Id).ToList(); 

//Get all employees having the above centercodids 
List<int> _employeeIds = _db.EmployeeCenters 
         .Where(x => _centerCodeIds.Any(cc => x.Id == cc)) 
         .Select(x => x.Employee.Id).ToList(); 
+0

@teovankot my bad;) –

回答

1

這就是:

List<int> _employeeIds = _db.EmployeeCenters.Where(x => _db.EmployeeCenters 
         .Where(y => y.EmployeeId == 40) 
         .Select(y => y.CenterCodeId) 
         .Contains(x.CenterCodeId)) 
         .Select(x => x.EmployeeId) 
         .ToList(); 

但你肯定您需要在查詢中選擇第二個子選擇?