.net core - Add ICollection list of User to Existing Roles List in C# -


i have user , role classes, each list define. want add list of users specific role, in other word want put list of user reference in icollection role.

user

 public class user {     public int userid { get; set; }     public string name { get; set; }     public string address { get; set; }     public  bool activestatus { get; set; }       public list<user> users = new list<user>();    public list<user> builduserrepository()     {         users.add(new user { userid = 01, name = "khurram", address = "london", activestatus = true });         users.add(new user { userid = 02, name = "sana", address = "london", activestatus = true });         users.add(new user { userid = 03, name = "richard", address = "london", activestatus = false });         users.add(new user { userid = 04, name = "tracy", address = "london", activestatus = true });         users.add(new user { userid = 05, name = "laura", address = "manchester", activestatus = true });         users.add(new user { userid = 06, name = "james", address = "london", activestatus = false });          return users;     } } 

role

public class role {     public int roleid { get; set; }     public string roletitle { get; set; }     public icollection<user> usersinrole { get; set; }      public list<role> roles = new list<role>();      public void buildrolerepository()     {         roles.add(new role { roleid = 01, roletitle = "admin" });         roles.add(new role { roleid = 02, roletitle = "management" });         roles.add(new role { roleid = 03, roletitle = "user" });     }  } 

now want add example 2 user of userid 01 , 02 role icollection roleid = 01, how can this

without drastic changes design of solution, can accomplish following code snippet below. remember hard coding of actual/testing data inside classes introducing lot of limitations further on , considered anti-pattern. might better off using external data storage (file or database) , repository pattern persistence layer. hope helps!

role.roles.first(x => x.roleid == 01)     .usersinrole.addrange(         user.users.where(x => x.userid == 01 || x.userid == 02));  public class user {     public int userid { get; set; }     public string name { get; set; }     public string address { get; set; }     public bool activestatus { get; set; }      public static list<user> users = new list<user>     {         new user {userid = 01, name = "khurram", address = "london", activestatus = true},         new user {userid = 02, name = "sana", address = "london", activestatus = true},         new user {userid = 03, name = "richard", address = "london", activestatus = false},         new user {userid = 04, name = "tracy", address = "london", activestatus = true},         new user {userid = 05, name = "laura", address = "manchester", activestatus = true},         new user {userid = 06, name = "james", address = "london", activestatus = false}     }; }  public class role {     public int roleid { get; set; }     public string roletitle { get; set; }     public list<user> usersinrole { get; set; }      public static list<role> roles = new list<role>     {         new role {roleid = 01, roletitle = "admin"},         new role {roleid = 02, roletitle = "management"},         new role {roleid = 03, roletitle = "user"}     }; } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -