使用ASP.NET Core中的联接查询在实体框架中获取用户角色

时间:2018-12-25 17:30:38

标签: entity-framework asp.net-core jointable

我有4个表,分别称为Users,Roles,Photos和UserRelationships。用户和角色表是身份服务器表。我想知道的是我想返回带有角色名称的用户列表。我可以获取其他详细信息,但是在获取角色名称时会卡住。

这是我自己创建的表格的屏幕截图

照片 Click here to view image

UserRelationships Click here to view image

这是我尝试过的代码

var relations = await (from relation in _context.UserRelationships
                                   where (relation.RelatingUserId == id || relation.RelatedUserId == id)
                                    && (relation.Status == UserRelationShipStatus.Active)
                                    && (relation.Type != UserRelationshipType.Blocked)

                                   select new
                                   {
                                       RelationId = relation.Id,
                                       User = relation.RelatedUserId == id ? (from usr in _context.Users
                                                                              join photo in _context.Photos on usr.Id equals photo.UserId
                                                                              where usr.Id == relation.RelatingUserId && photo.IsMain == true
                                                                              select new
                                                                              {
                                                                                  UserId = usr.Id,
                                                                                  UserName = usr.UserName,
                                                                                  PhotoUrl = photo.Url,
                                                                                  Role = ?
                                                                              })
                                                       : (from usr in _context.Users
                                                          join photo in _context.Photos on usr.Id equals photo.UserId
                                                          where usr.Id == relation.RelatedUserId && photo.IsMain == true
                                                          select new
                                                          {
                                                              UserId = usr.Id,
                                                              UserName = usr.UserName,
                                                              PhotoUrl = photo.Url,
                                                              Role = ?
                                                          })

                                   }
                                  ).ToListAsync();

我在这里做的是在UserRelationships表中包含两个名为RelatingUserId和RelatedUserId的字段,这意味着关系发送用户和接收用户。如果传递的UserId等于这些字段之一,那么我想获取该用户详细信息。我想知道的是,当获得用户角色时该怎么做。

非常感谢

1 个答案:

答案 0 :(得分:0)

您无法在Linq查询中检索数据。尝试使用ViewModel而不是匿名类型返回结果。然后通过检索用户角色将Roles添加到结果中。

1。创建ViewModel

public class UserRelationshipsViewModel
{
    public string RelationId { get; set; }
    public UserViewModel User { get; set; }

}

public class UserViewModel
{
    public string UserId { get; set; }
    public string UserName { get; set; }
    public string PhotoUrl {get; set;}

    public IList<string> Roles { get; set; }
}

2。使用ViewModel返回数据

var relations = await (from relation in _context.UserRelationships
                               ...
                               select new UserRelationshipsViewModel
                               {
                                   RelationId = relation.Id,
                                   User = relation.RelatedUserId == id ? (from usr in _context.Users
                                                                          join photo in _context.Photos on usr.Id equals photo.UserId
                                                                          where usr.Id == relation.RelatingUserId && photo.IsMain == true
                                                                          select new UserViewModel
                                                                          {
                                                                              UserId = usr.Id,
                                                                              UserName = usr.UserName,
                                                                              PhotoUrl = photo.Url,

                                                                          })
                                                   : (from usr in _context.Users
                                                      join photo in _context.Photos on usr.Id equals photo.UserId
                                                      where usr.Id == relation.RelatedUserId && photo.IsMain == true
                                                      select new UserViewModel
                                                      {
                                                          UserId = usr.Id,
                                                          UserName = usr.UserName,
                                                          PhotoUrl = photo.Url,

                                                      })

                               }
                              ).ToListAsync();

3。向数据添加角色。

 foreach (var r in relations)
        {
            var user = await _userManager.FindByIdAsync(r.User.UserId);
            var Roles = await _userManager.GetRolesAsync(user);
            r.User.Roles = Roles;
        }

更新:

成千上万的用户时,这将导致性能下降。我试图加入UserRoles表来获取角色。

              (from usr in _context.Users

                    join ur in _context.UserRoles on usr.Id equals ur.UserId into bt
                    join photo in _context.Photos on usr.Id equals photo.UserId
                    where usr.Id == relation.RelatingUserId && photo.IsMain == true
                    select new
                    {

                        Roles = (from x in bt
                                join r in _context.Roles on x.RoleId equals r.Id
                                select new
                                {
                                    r.Name
                                }
                                ).ToList()

                    })