sqlserver 用戶權(quán)限管理,LINQ去除它的重復(fù)菜單項(xiàng)
更新時(shí)間:2011年08月28日 22:03:43 作者:
事情是這樣的,我有三張表,用戶_角色關(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)用戶登陸后,通過UserId得到User_Role列表,將用戶所包括的角色得出
通過User_Role找到所有對(duì)應(yīng)Menu
現(xiàn)在有個(gè)問題,就是一個(gè)用戶可以有多少角色,一個(gè)角色有多個(gè)菜單,當(dāng)然,兩個(gè)不同的角色可以有相當(dāng)?shù)牟藛雾?xiàng),這時(shí),就出現(xiàn)一個(gè)問題,用戶在“管理員”這個(gè)角色里有“文件”這個(gè)菜單,同時(shí)它在“新聞管理員”這個(gè)角色里也有“文件”這個(gè)菜單,這樣返回就會(huì)出現(xiàn)兩個(gè)完成相同的”文件“菜單,下面,我使用匿名類和distinct方法來解決這個(gè)問題,代碼如下:
class Program
{
static void Main(string[] args)
{
#region 實(shí)體列表初始化
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 實(shí)體對(duì)象
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找到所有對(duì)應(yīng)Menu
現(xiàn)在有個(gè)問題,就是一個(gè)用戶可以有多少角色,一個(gè)角色有多個(gè)菜單,當(dāng)然,兩個(gè)不同的角色可以有相當(dāng)?shù)牟藛雾?xiàng),這時(shí),就出現(xiàn)一個(gè)問題,用戶在“管理員”這個(gè)角色里有“文件”這個(gè)菜單,同時(shí)它在“新聞管理員”這個(gè)角色里也有“文件”這個(gè)菜單,這樣返回就會(huì)出現(xiàn)兩個(gè)完成相同的”文件“菜單,下面,我使用匿名類和distinct方法來解決這個(gè)問題,代碼如下:
復(fù)制代碼 代碼如下:
class Program
{
static void Main(string[] args)
{
#region 實(shí)體列表初始化
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 實(shí)體對(duì)象
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 update 觸發(fā)器 可獲得被update的行的信息
sql update 觸發(fā)器 可獲得被update的行的信息,需要的朋友可以參考下。2010-06-0650個(gè)常用sql語句 網(wǎng)上流行的學(xué)生選課表的例子
這篇文字在網(wǎng)上被轉(zhuǎn)載爛了,里面有些sql適合用在應(yīng)用系統(tǒng)里,有些“報(bào)表”的感 覺更重些,主要是想復(fù)習(xí)前者2012-06-06SQL 比較一個(gè)集合是否在另一個(gè)集合里存在的方法分享
SQL 比較一個(gè)集合是否在另一個(gè)集合里存在的方法分享,需要的朋友可以參考下。2011-11-11在sqlserver中如何使用CTE解決復(fù)雜查詢問題
本文給大家介紹使用cte解決復(fù)雜查詢問題,在此代碼中需要注意count函數(shù),它統(tǒng)計(jì)了一個(gè)列,如果該列在某行的值為null,將不會(huì)統(tǒng)計(jì)該行,本文代碼詳解并附有注釋,感興趣的朋友一起看看吧2015-11-11