Menu, these three tables have the following relationships:
User_role=>roleid=>rolemenu
Rolemenu=>menuid=>menu
The business relationship between them is:
When the user logs in, the User_role list is obtained by UserID and the user's role is drawn
Find all corresponding menu through User_role
Now there's the question of how many roles a user can have, a role has more than one menu, of course, two different roles can have quite a menu item, at this time, there is a problem, the user in the "administrator" the role of the "file" this menu, and it in the "News manager" role also has "file" This menu, so that the return will appear with two complete the same "file" menu, I use the anonymous class and distinct method to solve this problem, the code is as follows:
Copy Code code as follows:
Class Program
{
static void Main (string[] args)
{
#region Entity List Initialization
list<user_role> userrole = new list<user_role>
{
New User_role ("01", 1),
New User_role ("01", 2),
New User_role ("02", 1),
};
list<role_menu> Rolemenu = new list<role_menu>
{
New Role_menu (2,3),
New Role_menu (1,1),
New Role_menu (1,2),
New Role_menu (2,1),
New Role_menu (2,2),
};
list<menu> Menu = new list<menu>
{
New Menu (1, "edit", 2),
New Menu (2, "file", 1),
New Menu (3, "View", 3),
New Menu (4, "System", 4),
};
#endregion
var linq = from Data1 in userrole
Join Data2 in Rolemenu on Data1. Roleid equals Data2. Roleid
Join Data3 in menu on Data2. MenuID equals Data3. MenuID
where Data1. Userid.equals ("01")
Select New
{
UserId = Data1. Userid
MenuID = data2. MenuID,
Menu = Data3,
};
Linq. Distinct (). by (I => i.menu.ordernumber). ToList ()
. ForEach (I => Console.WriteLine ("User id:{0}, Menu Id{1}, Menu name: {2}"
, I.userid, I.menuid, I.menu.menuname));
Console.readkey ();
}
}
#region Entity objects
Class User_role
{
public string UserId {get; set;}
public int Roleid {get; set;}
Public User_role (string userId, int roleid)
{
This. Roleid = Roleid;
This. UserId = UserId;
}
}
Class Menu
{
public int MenuID {get; set;}
public string MenuName {get; set;}
public int OrderNumber {get; set;}
Public Menu (int menuid, string menuName, int ordernumber)
{
This. MenuID = MenuID;
This. MenuName = MenuName;
This. OrderNumber = OrderNumber;
}
}
Class Role_menu
{
public int Roleid {get; set;}
public int MenuID {get; set;}
Public Role_menu (int roleid, int menuid)
{
This. Roleid = Roleid;
This. MenuID = MenuID;
}
}
#endregion
The result is what I would like to see: