最佳实践 - 将SQL查询转换为LINQ

时间:2013-05-03 22:02:51

标签: sql asp.net-mvc linq entity-framework

有人可以建议如何将下面的SQL转换为LINQ吗?

SELECT distinct EmployeeID
  FROM Employee,
       Business
 WHERE Administrator = 1 or 
         (BusinessID = 1 and
         (EmployeeID = Initiator or
          EmployeeID = Owner))

或者可能建议使用多个表达式获得相同结果的更好方法?我读过LINQ可能有问题,所以我认为查询越复杂,问题就越可能发生?

1 个答案:

答案 0 :(得分:5)

听起来你只想:

var query = (from employee in db.Employee
             from business in db.Business
             where employee.Administrator == 1 ||
                 (business.BusinessId == 1 &&
                  (employee.EmployeeId == business.Initiator ||
                   employee.EmployeeId == business.Owner))
             select employee.EmployeeId).Distinct();

我已经猜到哪个列属于哪个表...