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

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -