Reputation: 165
I have this table
public class Unity
{
public int Id {get;set }
public string Name{ get; set; }
}
public class UsersRight
{
public int Id {get;set }
public string Name{ get; set; }
public int Value{ get; set; }
}
I need a list of all unities that user have access.
I know to do this way:
var userRight = _DAL.UserRights(user).ToList();
var listUser = new List<Unity>;
foreach (var item in userRight)
{
listUser.add( new Unity(Name = item.Name, Id = item.Value));
}
How can I do this in a more efficient way?
Upvotes: 0
Views: 119
Reputation: 116
In your scenario, the User entity should have a list of Unities:
public virtual ICollection<Unity> Unities { get; set; }
and the Unity entity should have a User:
public virtual User User { get; set; }
You can check this entity framework tutorial on how to configure one-to-many relationship.
Upvotes: 3