dotnet core 身份映射 List<AppUser> 到 IList<UserDto>

dotnet core Identiry map IList<AppUser> to IList<UserDto>

我想获取具有教练角色的用户列表。我的查询处理程序是这样的:

namespace Application.Coach.Queries.CoachList
{
    public class CoachListQueryHandler : IRequestHandler<CoachListQuery, CoachListVm>
    {
        private readonly CoachDataContext _context;
        private readonly IMapper _mapper;
        private readonly UserManager<AppUser> _userManager;

        public CoachListQueryHandler(CoachDataContext context, IMapper mapper, UserManager<AppUser> userManager)
        {
            _context = context;
            _mapper = mapper;
            _userManager = userManager;
        }

        public async Task<CoachListVm> Handle(CoachListQuery request, CancellationToken cancellationToken)
        {
            var coaches = await _userManager.GetUsersInRoleAsync("Coach");
            if (coaches == null) throw new NotFoundException(nameof(Domain.Coach.Coach));

            return new CoachListVm
            {
                CoachList = coaches
            };
        }
    }
}

但是当我想 return CoachListVm:

CS0266 C# Cannot implicitly convert type to. An explicit conversion exists (are you missing a cast?)

这是 CoachListVm :

public class CoachListVm
    {
        public IList<CoachListDto> CoachList { get; set; }
    }

这是我的 CoachListDto :

public class CoachListDto : IMapFrom<AppUser>
    {
        public string Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public bool IsApproved { get; set; }
        public ICollection<AppUserRole> UserRoles { get; set; }
        [Column(TypeName = "decimal(18,2)")] public decimal MinHourlyRate { get; set; }
        [Column(TypeName = "decimal(18,2)")] public decimal MaxHourlyRate { get; set; }

        public void Mapping(Profile profile)
        {
            profile.CreateMap<AppUser, CoachListDto>()
                .ForMember(d => d.IsApproved, opt => opt.MapFrom(s => s.Coach.IsApproved))
                .ForMember(d => d.MinHourlyRate, opt => opt.MapFrom(s => s.Coach.MinHourlyRate))
                .ForMember(d => d.MaxHourlyRate, opt => opt.MapFrom(s => s.Coach.MaxHourlyRate));
        }
    }

我的问题是如何将教练映射到 CoachLitVm?

您需要在 return 之前添加映射,如下所示

public async Task<CoachListVm> Handle(CoachListQuery request, CancellationToken cancellationToken)
{
    var appUsers = await _userManager.GetUsersInRoleAsync("Coach");
    if (appUsers == null) throw new NotFoundException(nameof(Domain.Coach.Coach));
    var coaches = _mapper.Map<IList<AppUser>, CoachListVm>(appUsers); //here what I mean
    
    return coaches;
}