如何简化查询以避免多次访问数据库

时间:2018-08-17 15:52:40

标签: c# entity-framework linq

如何避免多次调用存储库并简化使用联接的操作?

var members = await _groupMemberRepository.Value.GetAllGroupMembers(groupId);
var groupMemberModels = members as IGroupMemberModel[] ?? members.ToArray();
var users = await _userManager.Value.GetUsersList(groupMemberModels.Select(x => x.UserId));
var roles = await _userAccountBusinessLogic.GetRoles(null);
return (from m in groupMemberModels
    join u in users
        on m.UserId equals u.Id
    select
        new Member
        {
            UserId = m.UserId,
            FirstName = u.FirstName,
            LastName = u.LastName,
            Username = u.UserName,
            GroupUserType = m.GroupUserType,
            LastLoggedIn = u.LastLoggedIn,
            Roles = MeeRoleParser.ParseList(u.Roles.Select(x =>
                roles.Where(r => r.Id == x.RoleId).Select(r => r.Name).FirstOrDefault()).ToList())
        }).ToArray();

以上查询调用了不同的存储库。我不知道如何进行加入并简化通话

0 个答案:

没有答案