Automapper 映射到元组,第一个元素是列表,第二个元素很简单 属性

Automapper map to tuple which first element is list second one is simple property

我需要映射此结果,但出现错误: AutoMapper.AutoMapperConfigurationException: 'Custom configuration for members is only supported for top-level individual members on a type.'

public class ResponseModel
    {
        public int TotalPageCount { get; set; }
        public IEnumerable<ExternalAPIResponseModel> ExternalAPIResponse { get; set; } = new List<ExternalAPIResponseModel>();
    }


 public class ExternalAPIResponseModel
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string JobFunction { get; set; }
        public string JobLevel { get; set; }
        public string CompanyName { get; set; }
        public string State { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string Address { get; set; }
    }


 public class Contact
    {
        public int Ocid { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        // etc.
    }

 
//I know the mistake is here
CreateMap<(IEnumerable<Contact> Contacts, int pagesCount), ResponseModel>()
               .ForMember(dest => dest.ExternalAPIResponse.Select( x=>new {
                       x.Address,
                       x.CompanyName,
                       x.FirstName,
                       x.LastName,
                       x.JobFunction,
                       x.JobLevel,
                       x.Email,
                       x.Phone,
                       x.State
                   }),
                   
                    opt => opt.MapFrom(src => src.Contacts.Select(c=> new { 
                    c.ContactAddress1,
                    c.HqcompanyName,
                    c.FirstName,
                    c.LastName,
                    c.IdjobFunctionNavigation.JobFunction1,
                    c.IdjobLevelNavigation.JobLevel1,
                    c.Email,
                    c.ContactPhone,
                    c.ContactState })))
                         
               .ForMember(dest => dest.TotalPageCount, opt => opt.MapFrom(src => src.pagesCount));
   
    //I call it from conntroller like this 
    var mappedResult = _mapper.Map<ResponseModel>(result);

如何将 (IEnumerable Contacts, int pagesCount) 映射到 ResponseModel 以避免出错?

使用Tuple Tuple<IEnumerable<Contact>, int>然后映射到ResponseModel

映射器

   CreateMap<Contact, ExternalAPIResponseModel>()
        .ForMember(dest => dest.FirstName, opt => opt.MapFrom(source => source.FirstName))
        .ForMember(dest => dest.LastName, opt => opt.MapFrom(source => source.LastName))
        .ForMember(dest => dest.CompanyName, opt => opt.MapFrom(source => source.HqcompanyName))
        .ForMember(dest => dest.Address, opt => opt.MapFrom(source => source.ContactAddress1))
        .ForMember(dest => dest.JobFunction, opt => opt.MapFrom(source => source.IdjobFunctionNavigation.JobFunction1))
        .ForMember(dest => dest.JobLevel, opt => opt.MapFrom(source => source.IdjobLevelNavigation.JobLevel1))
        .ForMember(dest => dest.Email, opt => opt.MapFrom(source => source.Email))
        .ForMember(dest => dest.Phone, opt => opt.MapFrom(source => source.ContactPhone))
        .ForMember(dest => dest.State, opt => opt.MapFrom(source => source.ContactState));

    CreateMap<Tuple<IEnumerable<Contact>, int>, ResponseModel>()
        .ForMember(dest => dest.ExternalAPIResponse, opt => opt.MapFrom(source => source.Item1))
        .ForMember(dest => dest.TotalPageCount, opt => opt.MapFrom(source => source.Item2));

在控制器中

int pagesCount = 10;
IEnumerable<Contact> contacts = GetListOfContacts(pagesCount);
Tuple<IEnumerable<Contact>, int> result = Tuple.Create(contacts, pagesCount);
var mappedResult = _mapper.Map<ResponseModel>(result);