lambda expression join multiple tables with select and where clause

后端 未结 2 807
無奈伤痛
無奈伤痛 2020-12-28 15:25

I have three table many to many relationship I have joined the three table and select the value I want but now I need to select one row from the query result by where by spe

相关标签:
2条回答
  • 2020-12-28 15:41

    If I understand your questions correctly, all you need to do is add the .Where(m => m.r.u.UserId == 1):

        var UserInRole = db.UserProfiles.
            Join(db.UsersInRoles, u => u.UserId, uir => uir.UserId,
            (u, uir) => new { u, uir }).
            Join(db.Roles, r => r.uir.RoleId, ro => ro.RoleId, (r, ro) => new { r, ro })
            .Where(m => m.r.u.UserId == 1)
            .Select (m => new AddUserToRole
            {
                UserName = m.r.u.UserName,
                RoleName = m.ro.RoleName
            });
    

    Hope that helps.

    0 讨论(0)
  • 2020-12-28 15:46

    I was looking for something and I found this post. I post this code that managed many-to-many relationships in case someone needs it.

        var UserInRole = db.UsersInRoles.Include(u => u.UserProfile).Include(u => u.Roles)
        .Select (m => new 
        {
            UserName = u.UserProfile.UserName,
            RoleName = u.Roles.RoleName
        });
    
    0 讨论(0)
提交回复
热议问题