I need a way to create an extension method off of an IEnumerable that will allow me to return a list of SelectListItem’s.
For example
public class Role
{
public string Name {get;set;}
public string RoleUID {get;set;}
}
IEnumerable<Role> Roles = .../*Get Roles From Database*/
var selectItemList = Roles.ToSelectItemList(p => p.RoleUID,r => r.Name);
this would give me a SelectItemList with the Name being the display, and the RoleUID being the value.
IMPORTANT I want this to be generic so I can create it with any two properties off of an object, not just an object of type Role.
How can I do this?
I imagine something like the following
public static List<SelectItemList> ToSelectItemList<T,V,K>(this IEnumerable<T>,Func<T,V,K> k)
or something, I obviously know that would be incorrect.
Why not just combine the existing
SelectandToListmethods?If you want to specifically put it into one method then you could define
ToSelectListItemas a combination of those two methods.