没有正确回归

时间:2016-02-23 16:32:00

标签: c# arrays asp.net-mvc return iqueryable

我试图获取另一个列表中尚未提供的员工列表。但我只从数组列表中获取第一个元素。 例如:如果我尝试EmployeeId = new int[2] {5, 2};列表,仅排除'5'。所以请帮我纠正下面的代码。

public JsonResult GetEmployees(int[] EmployeeId)
    {            
        var dbs = new dbContext();

        if (EmployeeId != null)
        {
            foreach (var emp in EmployeeId)
            {
               var EmpList = dbs.Employees.Select(e => new
                   {
                       EmployeeId = e.EmployeeId,                          
                       Name = e.EmployeeName,
                       Job = e.Job.JobName,
                       Currency = e.Currency.CurrencyName,
                       Amount = e.Amount
                   }).Where(o => o.EmployeeId != emp);
              return Json(EmpList, JsonRequestBehavior.AllowGet);
            }
        }   
          return null
    }

2 个答案:

答案 0 :(得分:0)

您是否尝试过单步执行代码?

你的foreach迭代你的EmployeeId数组。 由于你在foreach中有一个return语句,它在那时退出函数,它只使用数组的第一个元素。

你需要这样的东西:

public JsonResult GetEmployees(int[] EmployeeId)
        {
            var dbs = new dbContext();

            if (EmployeeId != null)
            {                
                var EmpList = dbs.Employees.Where(EmployeeId.Contains(e.EmployeeId))
                .Select(e => new
                {
                    EmployeeId = e.EmployeeId,
                    Name = e.EmployeeName,
                    Job = e.Job.JobName,
                    Currency = e.Currency.CurrencyName,
                    Amount = e.Amount
                }).Where(o => o.EmployeeId != emp);

                return Json(EmpList, JsonRequestBehavior.AllowGet);               
            }

            return null;
        }

答案 1 :(得分:0)

试试这个:

var employeeList = dbs.Employees.
             .Where(e => EmployeeId.All(x=> x != e.EmployeeId))
             .Select(e => new
             {
               EmployeeId = e.EmployeeId,                          
               Name = e.EmployeeName,
               Job = e.Job.JobName,
               Currency = e.Currency.CurrencyName,
               Amount = e.Amount
             });
         return Json(EmpList, JsonRequestBehavior.AllowGet);
       }