自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用戶權(quán)限管理之用LINQ去除重復(fù)菜單項(xiàng)的操作詳解

數(shù)據(jù)庫(kù) SQL Server
本文我們主要介紹了SQL Server數(shù)據(jù)庫(kù)開發(fā)應(yīng)用程序時(shí)的用戶權(quán)限管理方面的操作,即用LINQ去除重復(fù)菜單項(xiàng)的詳細(xì)方法,希望本次的介紹能夠?qū)δ兴鶐椭?/div>

在SQL Server數(shù)據(jù)庫(kù)開發(fā)應(yīng)用程序時(shí),用LINQ去除重復(fù)菜單項(xiàng)的操作是怎么實(shí)現(xiàn)的呢?接下來(lái)我們通過(guò)一個(gè)實(shí)例來(lái)說(shuō)明這一過(guò)程。事情是這樣的,我有三張表,用戶_角色關(guān)系表User_Role,角色_菜單關(guān)系表Role_Menu和菜單表

Menu,這三個(gè)表之間有如下關(guān)系:

User_Role=>RoleId=>RoleMenu

RoleMenu=>MenuId=>Menu

它們之間的業(yè)務(wù)關(guān)系是:當(dāng)用戶登陸后,通過(guò)UserId得到User_Role列表,將用戶所包括的角色得出,通過(guò)User_Role找到所有對(duì)應(yīng)Menu。

現(xiàn)在有個(gè)問(wèn)題,就是一個(gè)用戶可以有多少角色,一個(gè)角色有多個(gè)菜單,當(dāng)然,兩個(gè)不同的角色可以有相當(dāng)?shù)牟藛雾?xiàng),這時(shí),就出現(xiàn)一個(gè)問(wèn)題,用戶在“管理員”這個(gè)角色里有“文件”這個(gè)菜單,同時(shí)它在“新聞管理員”這個(gè)角色里也有“文件”這個(gè)菜單,這樣返回就會(huì)出現(xiàn)兩個(gè)完成相同的”文件“菜單,下面,我使用匿名類和distinct方法來(lái)解決這個(gè)問(wèn)題,代碼如下:

  1. class Program  
  2. {  
  3. static void Main(string[] args)  
  4. {  
  5. #region 實(shí)體列表初始化  
  6. List<User_Role> userRole = new List<User_Role> 
  7. {  
  8. new User_Role("01",1),  
  9. new User_Role("01",2),  
  10. new User_Role("02",1),  
  11. };  
  12. List<Role_Menu> roleMenu = new List<Role_Menu> 
  13. {  
  14. new Role_Menu(2,3),  
  15. new Role_Menu(1,1),  
  16. new Role_Menu(1,2),  
  17. new Role_Menu(2,1),  
  18. new Role_Menu(2,2),  
  19. };  
  20. List<Menu> menu = new List<Menu> 
  21. {  
  22. new Menu(1,"編輯",2),  
  23. new Menu(2,"文件",1),  
  24. new Menu(3,"視圖",3),  
  25. new Menu(4,"系統(tǒng)",4),  
  26. };  
  27. #endregion  
  28. var linq = from data1 in userRole  
  29. join data2 in roleMenu on data1.RoleId equals data2.RoleId  
  30. join data3 in menu on data2.MenuId equals data3.MenuId  
  31. where data1.UserId.Equals("01")  
  32. select new  
  33. {  
  34. UserId = data1.UserId,  
  35. MenuId = data2.MenuId,  
  36. Menu = data3,  
  37. };  
  38. linq.Distinct().OrderBy(i => i.Menu.OrderNumber).ToList()  
  39. .ForEach(i => Console.WriteLine("用戶ID:{0},菜單ID{1},菜單名:{2}"  
  40. , i.UserId, i.MenuId, i.Menu.MenuName));  
  41. Console.ReadKey();  
  42. }  
  43. }  
  44. #region 實(shí)體對(duì)象  
  45. class User_Role  
  46. {  
  47. public string UserId { get; set; }  
  48. public int RoleId { get; set; }  
  49. public User_Role(string userId, int roleId)  
  50. {  
  51. this.RoleId = roleId;  
  52. this.UserId = userId;  
  53. }  
  54. }  
  55. class Menu  
  56. {  
  57. public int MenuId { get; set; }  
  58. public string MenuName { get; set; }  
  59. public int OrderNumber { get; set; }  
  60. public Menu(int menuId, string menuName, int orderNumber)  
  61. {  
  62. this.MenuId = menuId;  
  63. this.MenuName = menuName;  
  64. this.OrderNumber = orderNumber;  
  65. }  
  66. }  
  67. class Role_Menu  
  68. {  
  69. public int RoleId { get; set; }  
  70. public int MenuId { get; set; }  
  71. public Role_Menu(int roleId, int menuId)  
  72. {  
  73. this.RoleId = roleId;  
  74. this.MenuId = menuId;  
  75. }  
  76. }  
  77. #endregion 

這樣的結(jié)果是我希望看到的:

用戶權(quán)限管理之用LINQ去除重復(fù)菜單項(xiàng)的操作詳解

 關(guān)于用LINQ去除重復(fù)菜單項(xiàng)的相關(guān)知識(shí)就介紹到這里了,希望本次的介紹能夠?qū)δ兴鶐椭?/p>

【編輯推薦】

  1. 初學(xué)SQL Server數(shù)據(jù)庫(kù)的一些常用操作總結(jié)
  2. SQL Server數(shù)據(jù)庫(kù)創(chuàng)建數(shù)據(jù)倉(cāng)庫(kù)已分區(qū)表詳解
  3. SQL Server與Access數(shù)據(jù)庫(kù)ASP代碼的比較詳解
  4. SQL Server數(shù)據(jù)庫(kù)中bit字段類型使用時(shí)的注意事項(xiàng)
  5. SQL Server數(shù)據(jù)庫(kù)timestamp數(shù)據(jù)類型相關(guān)知識(shí)介紹
責(zé)任編輯:趙鵬 來(lái)源: 博客園
相關(guān)推薦

2009-07-15 13:31:51

Swing菜單和菜單項(xiàng)

2009-07-27 16:43:51

自定義Eclipse菜

2010-01-13 15:33:40

VB.NET菜單項(xiàng)目

2011-07-22 15:01:28

MongoDB權(quán)限管理

2009-08-27 09:38:02

Windows 7開始菜單

2011-01-11 14:06:39

2009-09-18 14:51:19

LINQ TO SQL

2023-09-27 23:57:21

2009-11-27 09:41:56

LINQ

2012-05-05 21:22:40

2010-09-27 14:36:24

SQL Server用

2009-10-29 15:56:12

Oracle用戶權(quán)限視

2010-10-29 10:46:23

Oracle超級(jí)用戶

2011-02-22 15:16:58

2011-07-26 14:57:39

2013-11-21 09:10:27

MongoDB

2017-03-20 19:01:20

Linux管理員系統(tǒng)用戶

2010-11-29 14:33:22

Sybase用戶管理

2010-03-19 16:07:41

Exchange 20

2009-03-18 09:45:32

教程管理用戶
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)