复制代码 代码如下:
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,"系统",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
这样的结果是我希望看到的:
新闻热点
疑难解答