Answer the question
In order to leave comments, you need to log in
How to formulate a LINQ query for a many-to-many relation in relation to EF?
There are the following entities:
Users:
UserId, Username
1, 'User 1'
2, 'User 2'
3, 'User 3'
Roles:
RoleId, Rolename
1, 'Role 1'
2, 'Role 2'
3, 'Role 3'
RoleUsers:
RoleUserId, RolePtr, UserPtr
1, 1, 1
2, 1, 2
3, 2, 1
4, 2, 2
5, 2, 3
6, 3, 3
With RoleUsers.RolePtr <-> Roles.RoleId and RoleUsers.UserPtr <-> Users.UserId created a many-to-many relationship between Users and Roles.
I can’t figure out how to formulate a query to get a pivot table with all Roles and Users relationships, so that later I can get the roles of a specific user by grouping. I want to do this with the least overhead for the application (let the database server do everything).
Result:
RoleUserId, RolePtr, UserPtr, Rolename, Username
1, 1, 1, 'Role 1', 'User 1'
2, 1, 2, 'Role 1', 'User 2'
3, 2, 1, 'Role 2' ', 'User 1'
4, 2, 2, 'Role 2', 'User 2'
5, 2, 3, 'Role 2', 'User 3'
6, 3, 3, 'Role 3', 'User 3 '
var query = from ...?
Answer the question
In order to leave comments, you need to log in
And it’s better to immediately ask the developers and ask for the source code stpsystems.com
Such a site is not difficult to implement on wp. Take any simplest theme, install a couple of plugins: the composer as the main one (not necessarily a paid-heavy visual composer, you can do something simpler and easier - Elementor Page Builder, for example), add Cool Timeline to it (or something like that ) to implement the same master page structure. And voila.
If you are talking specifically about EF, then everyone User
will have a property Roles
with a collection of roles, and vice versa, where the data you need is automatically grouped. The request can be written like this:
var query = DbContext.Users.Include(x => x.Roles).ToList();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question