将SQL转换为LINQ查询

时间:2013-09-05 11:32:11

标签: sql linq

您好我是LINQ的新手,并且要求将以下SQL转换为LINQ。 有人可以帮我怎么写吗?

select emp.id, emp.name from employee emp
left join department dept on dept.id = emp.deptid 
and (emp.managerid = '001' or (emp.manager in (select managerid from manager where location Contains('l001','l002','l003') and state =1)))

由于

1 个答案:

答案 0 :(得分:1)

也许是这样的:

var locationIds = new List<string> {"l001", "l002", "l003"};
var ls=(
    from emp in db.employee
    from dept in db.department
        .Where(a=>a.id == emp.deptid).DefaultIfEmpty()
    where (emp.managerid == "001" 
        || db.manager.Where(w=>locationIds.Contains(w.location) && w.state == 1)
                     .Select(s=>s.managerid).Contains(emp.manager))
    select new
        {
            emp.id, 
            emp.name
        }
).ToList();

其中db是linq数据上下文