如何添加要列出的条件?

时间:2018-11-14 00:21:18

标签: linq

使用下面的代码,我将获得员工的所有姓名。

如何添加where子句以仅将empStatus = 1的当前雇员获取?

 private void BindDropDownList()
        {
            var employees = dbEmployee.employeestable.Select(x => new
            {
                FullName = x.emp_first + " " + x.emp_last,
                Id = x.empid
            }).ToList();

            ViewBag.Employees = new SelectList(employees, "FullName", "FullName");
        }

1 个答案:

答案 0 :(得分:1)

Where之前添加Select过滤器

var employees = dbEmployee.employeestable
    .Where(x => x.empStatus == 1) //<----HERE
    .Select(x => new {
        FullName = x.emp_first + " " + x.emp_last,
        Id = x.empid
    })
    .ToList();

//...