Sqlserver user permission management, and LINQ removes its repeated menu items

Source: Internet
Author: User

Menu:
User_Role => RoleId => RoleMenu
RoleMenu => MenuId => Menu
The business relationship between them is:
After a user logs in, the user ID is used to obtain the User_Role list, and the roles included by the user are obtained.
Use User_Role to find all corresponding menus
Now there is a problem: how many roles a user can have and how many menus a role can have. Of course, two different roles can have equivalent menu items. In this case, a problem occurs, the user has the "file" menu in the "Administrator" role, and it also has the "file" menu in the "News administrator" role, in this case, two "file" menus with the same completion will appear. below, I use the Anonymous class and distinct method to solve this problem. The Code is as follows:
Copy codeThe Code is as follows:
Class Program
{
Static void Main (string [] args)
{
# Region Object 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 (). OrderBy (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 object
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 want to see:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.