lambda expression join multiple tables with select and where clause

后端 未结 2 820
無奈伤痛
無奈伤痛 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.

提交回复
热议问题