简体   繁体   中英

Using Linq to Entities to get a list of roles that a user has with a self join

I am trying to build a dynamic menu that uses Identity 2.0 roles to define what menu items the user should see. I am very new to C# and need some help please!

For the purpose of what I am doing, I am thinking of a Role as a Menu Item that a user can either see or not depending on the roles assigned to them. I have two levels of Menu Items- the parent menu item (such as "Reports" and the sub menu items (such as "Order Reports", "Receipt Reports" etc).

So my ApplicationRole class looks like this:

public string Description { get; set; }
public string MenuTitle {get;set;}
public string MenuIcon { get; set; }
public string ControllerName { get; set; }
public string ActionName { get; set; }

public virtual ApplicationRole ParentRole { get; set; }

I will only store roles against the user whereby the ParentRole is not null. I want to know retrieve this list of roles for the logged in user and render the menu.

So, the logic is going to be something like:

  1. Get a list of all Menu Items (roles) that the user has assigned against them.
  2. Using this list, use the ParentRole to get a distinct list of parent roles (the top level of the menu).
  3. Using these 2 lists, populate my View Model below:

     public class NavigationViewModel { public int MenuId { get; set; } public string MenuName { get; set; } public string ControllerName { get; set; } public string ActionName { get; set; } public string MenuIcon { get; set; } public IEnumerable<NavigationViewModel> ChildMenuItems { get; set; } } 

Basically, I am creating a menu that has a top level menu and a sub-menu.

My problem is, is that I really don't know where to start in terms of querying the database. Ideally, I would like a query that does a self join onto the Role table to get the Parent Role in the same query, however I'm brand new to C# and Linq and do not know where to start.

Any advice would be great.

My advice would be adding an ADO.NET Entity Data Model to your project and add the tables you want to query on from the DB. Then a .edmx file is created with your tables and their relations and you can access them in your code (a DbContext is created with your db name like YouDBNameEntities ).

Now that you are connected to the DB, create a new context:

YourDBNameEntities context = new YourDBNameEntities();

After that you can join on your added tables and in your case self join:

var Query = context.AddedTable.Join(context.AddedTable, ...)

or

var Query = from yourClassName1 in context.AddedTable

  join yourClassName2 in context.AddedTable on yourClass1.ID equals yourClass2.ID where !yourClassName1.ParentRole.equals(null) select new { ParentRole = yourClassName1.ParentRole } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM