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();
Source: stackoverflow.com
Related Query
- Linq : Select multiple values and assign to select with specific option/value
- Multiple Select and Join with LINQ and Lambda
- C# LINQ Select objects with the same value of one property join values of other
- Group items and select specific item from each group with LINQ
- Select most frequent value and count using LINQ and assign to dictionary
- LINQ building array with multiple values per select
- Linq left join with multiple columns and default values
- select from a list with field name and field value with linq (similar to select command)
- C# Linq to select multiple columns by group and apply sum(aggregate function) on value field at the same time
- C# LINQ Arrange Multiple Columns Value and Resign to Specific Columns
- Get the max date from linq with mutiple joins and with multiple select fields
- Linq select and add multiple values from the same object
- How to select from a list with Linq based on id and datetime value
- Select values from one table based on specific value of another table Linq
- Sort Generic list of generic objects by multiple values and specific value of generic object
- Select Single Value with ADO.Net Data Services and LINQ
- How do I use LINQ with GROUP BY and multiple MAX(...) to select multiple fields?
- How to set multiple property values with same name and counter value using reflection?
- Select values from multiple dictionaries into list of objects with LINQ
- Multiple Left Join with Linq and Defaults Values
- how to select records from multiple table with max count value from one table using Linq in Asp.net MVC C#
- How can I check null and assign new value in linq anonymous type to select expected sql result
- LINQ Query with multiple conditions and setting values into a list
- How to use LINQ to select object with minimum or maximum property value
- Select multiple records based on list of Id's with linq
- How to Select Min and Max date values in Linq Query
- How to select multiple values from a Dictionary using Linq as simple as possible
- lambda expression join multiple tables with select and where clause
- Select only the lowest values with Linq
- Preserve order of values with linq and ToLookup()
More Query from same tag
- How to query datas of Ara where location is "Budapest"?
- Optimization in the SingleOrDefault function of Linq
- Best way to use same object for adding records LINQ C#
- How to create index with nested list
- How do I rollback a failed InsertOnSubmit? (VB.NET LINQ)
- Compare two list based on condition C#
- Optimize LINQ query that runs fast in Sql server?
- How to display Model object properties in razor view VB.net
- How do I pass a value out of the index controller in MVC3?
- Handling Null Values in Controller
- Get SQL INSERT OUTPUT value in c#
- LINQ Join Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for
- LINQ - query Where method error
- Odata v4 ODataModelBuilder relations builder
- Check a list of DTOs using linq to see if all values that exclude a particular value all equal the same thing
- reversing text from input file using linq
- Joining returning null if one of the column is null
- include keyword not providing filter with extension method
- How to get latest record for each group using Entity Framework and MySQL including related entity
- Studying an Idea for extract random rows with LINQ
- Entity Framework Query: What's Happening Under the Covers Here?
- simple LINQ to XML query won't work
- Is LINQ to SQL deprecated?
- LINQ GroupBy a List according to a key inside dictionary inside dictionary inside List C#
- Why is LINQ to SQL entity association creating a new (duplicate) row when inserting a new record?
- Is it possible to combine these 2 linq queries?
- More Efficient LINQ Query
- Can LINQ be used in Dynamics CRM to get all Accounts not in a Collection?
- searching all subdirectories for certain files, skipping some folders
- Linq DataTable Having Query