如何编写下面的linq查询而不使用很多if条件?

时间:2013-03-31 10:47:32

标签: c# .net linq entity-framework

我根据级联下拉列表过滤员工数据。如果参数的值为“-1”,则表示“所有数据”。例如,如果departmentid = -1,则用户在下拉列表中选择了all,类似于类别和角色。

现在我需要使用EF linq查询来查询数据库。我的员工实体中有三个外键关联,用于根据所选值获取作业类别,角色和部门的名称。

因此,如果用户为类别和工作角色选择了-1(全部),则必须查询员工实体并带回所选部门中的所有员工,否则应根据所选部门或类别或作用。

我不知道如何在不使用三个或if-else语句的情况下为此编写简洁的linq查询。下面是我到目前为止所做的,但如果我采用这种方法,我需要使用至少另外三个if语句。

非常感谢任何帮助 - 谢谢

   [HttpGet]
    public ActionResult GetEmployees(Models.Department department, Models.Category      category, Models.Role role)
    {
        try
        {
           if (department.DepId == -1)
            {
                using (emsCtx)
                {
                    var allResults = (from q in emsCtx.Employees
                                      select new Models.Employee
                                      {
                                      Id = q.Id,
                                      FirstName = q.FirstName,
                                      MiddleName = q.MiddleName,
                                      LastName = q.Lastname,
                                      DOB = q.DOB,
                                      JobTitle = q.Roles.Designation,
                                      JobCategory = q.Categories.Type,
                                      Department = q.Departments.Name,
                                      Salary = q.BasicPay 
                                      }).ToList();
                    employeeData.Employees = allResults;
                }
            }
            else
            using (emsCtx)
            {
                var empResults = (from q in emsCtx.Employees
                                  where department.DepId == q.Department 
                                  && role.RoleId == q.Designation
                                  && category.CatId == q.Category
                                  select new Models.Employee
                                  {
                                      Id = q.Id,
                                      FirstName = q.FirstName,
                                      MiddleName = q.MiddleName,
                                      LastName = q.Lastname,
                                      DOB = q.DOB,
                                      JobTitle = q.Roles.Designation,
                                      JobCategory = q.Categories.Type,
                                      Department = q.Departments.Name,
                                      Salary = q.BasicPay 
                                  }).ToList();
                employeeData.Employees = empResults;
            }
            return View(employeeData);
        }

1 个答案:

答案 0 :(得分:0)

编写您的主LINQ表达式,如下所示:

from q in emsCtx.Employees
where (department.DepId == -1 ? true : (department.DepId == q.Department 
                                        && role.RoleId == q.Designation
                                        && category.CatId == q.Category))
select new Models.Employee

这将解决您的问题。