1

This is my sample query

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

How can we acheive the above using 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();
teo van kot
  • 12,350
  • 10
  • 38
  • 70
ksg
  • 3,927
  • 7
  • 51
  • 97

1 Answers1

1

Here it is:

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

But are you sure that you need second sub select in your Query?

teo van kot
  • 12,350
  • 10
  • 38
  • 70