score:2

Accepted answer

You just need to correct a little so that the code will look like this:

var employeesList = dbdev.employeeDetails.OrderBy(x => x.employeeID)
                         .AsEnumerable()
                         .Select(x => new {
                              Name = String.Format("{0} {1} {2}", x.employeeNumber, x.knownas, x.surname), 
                              Value = x.employeeID
                         }).ToList();

Alternative by using query expression:

var employeesList = (from x in dbdev.employeeDetails.AsEnumerable()
                     orderby x.employeeID ascending
                     select new {
                         Name = String.Format("{0} {1} {2}", x.employeeNumber, x.knownas, x.surname), 
                         Value = x.employeeID
                     }).ToList();

Then create a SelectList using existing list:

SelectList employeeIDList = new SelectList(employeesList, "Value", "Name", userAccess.employeeID);

Or... just create a IEnumerable<SelectListItem> directly and pass it to DropDownListFor helper:

var employeesList = dbdev.employeeDetails.OrderBy(x => x.employeeID)
                         .AsEnumerable()
                         .Select(x => new SelectListItem {
                              Text = String.Format("{0} {1} {2}", x.employeeNumber, x.knownas, x.surname), 
                              Value = x.employeeID
                         }).ToList();

Related Articles