ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

CreateSelectList

Convert any list of objects to a select list

Source

public static IEnumerable<SelectListItem> CreateSelectList<T>(this List<T> entities, Func<T, object> value, Func<T, object> text)
{
    return entities.Select(x => new SelectListItem
    {
        Value = value(x).ToString(),
        Text = text(x).ToString()
    });
}

Example

productList.CreateSelectList(x => x.Id, x => x.Name)

Author: Djordje Djukic

Submitted on: 8 jul. 2016

Language: C#

Type: IEnumerable<SelectListItem>

Views: 3608