c# - How to Cast String Array To Ilist -
i have class :
public class user { public int id; public icollection<role> roles; } and have object :
user userobj = new user { id = 1 }; i want set array :
rolesvm.items.split(new string[] { "\r\n" }, stringsplitoptions.none); for :
userobj.roles when assign string[] array roles, i'm getting error:
cannot implicitly convert type 'string[]' 'system.collections.generic.icollection
how can ?
there no magical way transform string other type. need call code creates objects of type somehow.
i.e. if role have constructor taking string argument:
userobj.roles = rolesvm.items.split(new string[] {"\r\n" }, stringsplitoptions.none) .select(value => new role(value)) // "convert" strings `role` .tolist(); // transform enumerable type implements icollection (list) depending on role type new role(value) may need replaced other conversion new role{ value = value} or enums - enum.parse(typeof(role), value).
Comments
Post a Comment