anatp_123
anatp_123

Reputation: 1205

How to add where condition to list?

With the below code I get all the names of the employee.

How can I add a where clause to only get current employees 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");
        }

Upvotes: 1

Views: 38

Answers (1)

Nkosi
Nkosi

Reputation: 246998

Add a Where filter before the 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();

//...

Upvotes: 1

Related Questions