Entity Framework - recording a query using a lambda expression

I just ran Entity Framework and linq and wrote this query

 var query = from rp in db.UM_RolePermission
             where (from ru in db.UM_RoleUser 
             where ru.UM_User.UserID == userId select ru.RoleID).Contains(rp.RoleId)
             select rp;

The above works fine and completely fills my need, however I am trying to write the same using the lambda expression to understand this.

I tried to write this, but I could not complete it.

var query1 = db.UM_RolePermission
             .Where(rp => (from ru in db.UM_RoleUser where ru.UM_User.UserID == userId select ru.RoleID).Contains(rp.RoleId));

Can anyone do this?

RelationShip:

UM_RoleUser and UM_User

thank

+3
source share
2 answers
var query = db.UM_RolePermission
            .Where(rp => db.UM_RoleUser
                         .Where(ru => ru.UM_User.UserID == userId)
                         .Select(ru => ru.RoleID)
                         .Contains(rp.RoleId))
+2
source

Am I going to move forward and assume that you have defined the relationship between RolePermission and RoleUser in a many-to-many relationship? It will make your life a lot easier.

var query1 = db.UM_RoleUser
    .Where(ru => ru.UserId == userID)
    .SelectMany(rp => rp.RolePermissions);

, , .

+1

Source: https://habr.com/ru/post/1759866/


All Articles