人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

sqlserver 用戶權限管理,LINQ去除它的重復菜單項

發(fā)布日期:2022-01-25 14:26 | 文章來源:gibhub

User_Role=>RoleId=>RoleMenu
RoleMenu=>MenuId=>Menu
它們之間的業(yè)務關系是:
當用戶登陸后,通過UserId得到User_Role列表,將用戶所包括的角色得出
通過User_Role找到所有對應Menu
現(xiàn)在有個問題,就是一個用戶可以有多少角色,一個角色有多個菜單,當然,兩個不同的角色可以有相當?shù)牟藛雾?,這時,就出現(xiàn)一個問題,用戶在“管理員”這個角色里有“文件”這個菜單,同時它在“新聞管理員”這個角色里也有“文件”這個菜單,這樣返回就會出現(xiàn)兩個完成相同的”文件“菜單,下面,我使用匿名類和distinct方法來解決這個問題,代碼如下:
復制代碼 代碼如下:

class Program
{
static void Main(string[] args)
{
#region 實體列表初始化
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,"編輯",2),
new Menu(2,"文件",1),
new Menu(3,"視圖",3),
new Menu(4,"系統(tǒng)",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("用戶ID:{0},菜單ID{1},菜單名:{2}"
, i.UserId, i.MenuId, i.Menu.MenuName));
Console.ReadKey();
}
}
#region 實體對象
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

這樣的結果是我希望看到的:

image

版權聲明:本站文章來源標注為YINGSOO的內(nèi)容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務

7x24全年不間斷在線

專屬顧問服務

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務熱線

關注
微信

關注官方微信
頂部