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 Articles
- Linq : Select multiple values and assign to select with specific option/value
- C# LINQ Select objects with the same value of one property join values of other
- LINQ building array with multiple values per select
- Select values from one table based on specific value of another table Linq
- Select values from multiple dictionaries into list of objects with LINQ
- how to select records from multiple table with max count value from one table using Linq in Asp.net MVC C#
- 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 multiple values from a Dictionary using Linq as simple as possible
- Select only the lowest values with Linq
- Using Linq to do a Contains with multiple values
- linq how to select a parent with a child collection that contains one or many of an array (or list) of values
- How do I use Linq ToDictionary to return a dictionary with multiple values in the dictionary items?
- Linq GroupBy on multiple columns with potentials null value
- Multiple Select and Join with LINQ and Lambda
- LINQ Select Multiple value
- Select all rows with distinct column value using LINQ
- C# Create object with dynamic properties : LINQ select List<object> values by property names array
- How can I make my Linq select return values if the value selected is null?
- Group items and select specific item from each group with LINQ
- LINQ grouping multiple fields only if one of the fields is a specific value
- Select most frequent value and count using LINQ and assign to dictionary
- C# LINQ selecting a list with a property that has multiple value
- LINQ querying nested dictionary with multiple key value selection in C#
- Select multiple rows in order based on list of Ids with LINQ
- LINQ - Multiple left joins with nullable values
- LINQ remove items with specific value in group by
- Select data from multiple unrelated tables with LINQ to Entity Framework
- Linq select distinct values with comparison
- Select properties with specific attribute value
- Optimization of correlated subqueries with Automapper
- SQL Server Stored Procedure Multiple Condtions
- Linq context behaviour
- Dynamically build query for Azure DocumentDB
- How do I rewrite this Linq query to avoid using FirstOrDefault?
- Combine 2 LINQ queries into 2
- How to use a LINQ where clause on a list of Func delegates
- Flatten a query of multiple records to one object
- KeyValuePair with repeated Keys
- Cluster items by condition and order using linq
- Overlapping records between two dates using Linq
- System.NotSupportedException: Unable to create a constant value of type_.Only primitive and...this context
- how to do this using linq?
- Is it possible to put two Lambda expressions inside a single query?
- Using Linq to extract data from XML
- How to construct dynamic LINQ query string be for a query that looks like *abc*def*
- randomize Select do not work
- Expression.Body.Expressions -- how to use it?
- How can I sort a 2d array using Linq?
- Include and Where predicate cause left join instead of inner join