sqlserver 用戶權(quán)限管理,LINQ去除它的重復(fù)菜單項
更新時間:2011年08月28日 22:03:43 作者:
事情是這樣的,我有三張表,用戶_角色關(guān)系表User_Role,角色_菜單關(guān)系表Role_Menu和菜單表
Menu,這三個表之間有如下關(guān)系:
User_Role=>RoleId=>RoleMenu
RoleMenu=>MenuId=>Menu
它們之間的業(yè)務(wù)關(guān)系是:
當(dāng)用戶登陸后,通過UserId得到User_Role列表,將用戶所包括的角色得出
通過User_Role找到所有對應(yīng)Menu
現(xiàn)在有個問題,就是一個用戶可以有多少角色,一個角色有多個菜單,當(dāng)然,兩個不同的角色可以有相當(dāng)?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
User_Role=>RoleId=>RoleMenu
RoleMenu=>MenuId=>Menu
它們之間的業(yè)務(wù)關(guān)系是:
當(dāng)用戶登陸后,通過UserId得到User_Role列表,將用戶所包括的角色得出
通過User_Role找到所有對應(yīng)Menu
現(xiàn)在有個問題,就是一個用戶可以有多少角色,一個角色有多個菜單,當(dāng)然,兩個不同的角色可以有相當(dāng)?shù)牟藛雾?,這時,就出現(xiàn)一個問題,用戶在“管理員”這個角色里有“文件”這個菜單,同時它在“新聞管理員”這個角色里也有“文件”這個菜單,這樣返回就會出現(xiàn)兩個完成相同的”文件“菜單,下面,我使用匿名類和distinct方法來解決這個問題,代碼如下:
復(fù)制代碼 代碼如下:
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
這樣的結(jié)果是我希望看到的:
相關(guān)文章
如何在 SQL SERVER 中快速有條件刪除海量數(shù)據(jù)
如何在 SQL SERVER 中快速有條件刪除海量數(shù)據(jù)...2006-12-12
SQL Server中實現(xiàn)自定義數(shù)據(jù)加密功能
在當(dāng)今數(shù)字化時代,數(shù)據(jù)安全已成為企業(yè)和個人最為關(guān)注的問題之一,SQL Server提供了多種數(shù)據(jù)加密技術(shù),包括透明數(shù)據(jù)加密(TDE)、備份加密以及列級加密等,本文將詳細介紹如何在SQL Server中實現(xiàn)自定義數(shù)據(jù)加密功能,需要的朋友可以參考下2024-08-08
在SQL Server 2005中創(chuàng)建CLR存儲過程的詳細介紹
本篇文章是對在SQL Server 2005中創(chuàng)建CLR存儲過程進行了詳細的分析介紹,需要的朋友參考下2013-06-06
SqlServer中用exec處理sql字符串中含有變量的小例子
這篇文章主要介紹了用exec處理sql字符串中含有變量的小例子,有需要的朋友可以參考一下2013-12-12
SQL Server數(shù)據(jù)庫的高性能優(yōu)化經(jīng)驗總結(jié)
小編以前在做ASP及.NET的時候經(jīng)常用到SQL SERVER,現(xiàn)在用PHP雖然大多數(shù)時候用MYSQL,但不泛有些客戶要在原來SQL的平臺上升級或兼容開發(fā),值得慶幸的是PHP無所不能,基本上所有的數(shù)據(jù)庫它都能連接并支持2011-07-07
sql自動增長標(biāo)識導(dǎo)致導(dǎo)入數(shù)據(jù)問題的解決方法
對于一個設(shè)了自動增長標(biāo)識的數(shù)據(jù)表來說,它的字段的值是由數(shù)據(jù)庫自動設(shè)置的;這在導(dǎo)數(shù)據(jù)時很麻煩2012-11-11
掌握SQL?Server實戰(zhàn)教程之SQL?Server的安裝指南
對于項目來說最重要的一環(huán)莫過于數(shù)據(jù)庫了,一個良好的數(shù)據(jù)庫能夠讓項目更加清晰、條理分明,下面這篇文章主要給大家介紹了關(guān)于掌握SQL?Server實戰(zhàn)教程之SQL?Server的安裝指南,需要的朋友可以參考下2023-04-04
將Excel數(shù)據(jù)導(dǎo)入到SQL?Server數(shù)據(jù)庫的操作指南
這篇文章主要介紹了將Excel數(shù)據(jù)導(dǎo)入到SQL?Server數(shù)據(jù)庫的操作指南,文中通過圖文結(jié)合的方式給大家講解的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-08-08


