R
R
Roman Koff2016-09-07 17:11:01
WordPress
Roman Koff, 2016-09-07 17:11:01

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

5 answer(s)
D
Dmitry Murzinov, 2016-07-20
@iDoka

this is never WP

A
Alexander, 2016-07-20
@2gud

And it’s better to immediately ask the developers and ask for the source code stpsystems.com

K
Ksenia Mikhailova, 2016-07-20
@arizona

This is clearly not WordPress.6dbbf0dbd83041f1a689356cc0245723.png

R
R. Khalnepes, 2016-07-21
@Yomud

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.

A
Andrew, 2016-09-08
@impwx

If you are talking specifically about EF, then everyone Userwill have a property Roleswith 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 question

Ask a Question

731 491 924 answers to any question