如何使用自动映射器将 IEnumerable<Model> 映射到具有 IEnumerable<AnotherDto> 属性 的 DTO?

How to map IEnumerable<Model> to a DTO with IEnumerable<AnotherDto> property using automapper?

我有这些模型和 DTO

public class ServiceModel
{
    public string ServiceId { get; set; }
    public string ServiceName { get; set; }
    
    public string ServiceTypeId { get; set; }
    public double QuarterlyCharge { get; set; }
    public double AnnualCharge { get; set; }
}

public class ServiceResponseDto
{
    public string ServiceId { get; set; }
    public string ServiceName { get; set; }
    public IEnumerable<TaxDto> Tax { get; set; }
}

public class TaxDto
{
    public string ServiceTypeId { get; set; }
    public double QuarterlyCharge { get; set; }
    public double AnnualCharge { get; set; }
}

我有 ServiceModel 的 IEnumerable 导致:

ServiceId ServiceName ServiceTypeId AnnualCharge QuarterlyChar
1 Service 1 1 1
1 Service 2 2 2
1 Service 3 3 3

如何将其映射到 ServiceResponseDto

这是我目前的解决方案

IEnumerable<IGrouping<string, ServiceModel>> groupedservice = serviceQueryResult.GroupBy(c => c.ServiceId);
ServiceResponseDto mappedService = _mapper.Map<IEnumerable<IGrouping<string, ServiceModel>>, ServiceResponseDto >(groupedservice );

我的映射:

CreateMap<IEnumerable<IGrouping<string, ServiceModel>>, ServiceResponseDto>()
    .ForMember(dest => dest.ServiceId, opt => opt.MapFrom(src => src.FirstOrDefault().Key))
    .ForMember(dest => dest.ServiceName, opt => opt.MapFrom(src => src.FirstOrDefault().FirstOrDefault().ServiceName))
    .ForMember(dest => dest.Tax , opt => opt.MapFrom(src => src.Select(c => new TaxDto() { ServiceTypeId  = c.FirstOrDefault().ServiceTypeId , QuarterlyCharge = c.FirstOrDefault().QuarterlyCharge, AnnualCharge  = c.FirstOrDefault().AnnualCharge } )));

预期结果:

new ServiceResponseDto()
{
    ServiceId  = "1",
    ServiceName = "Service"
    Tax = new IEnumerable<TaxDto>()
    {
        new = TaxDto(){ServiceTypeId=ServiceType1, QuarterlyCharge =1, AnnualCharge=1},
        new = TaxDto(){ServiceTypeId=ServiceType2, QuarterlyCharge =2, AnnualCharge=2},
        new = TaxDto(){ServiceTypeId=ServiceType3, QuarterlyCharge =3, AnnualCharge=3},
    }
}

实际结果:映射后只出现1条记录

new ServiceResponseDto()
{
    ServiceId  = "1",
    ServiceName = "Service"
    Tax = new IEnumerable<TaxDto>()
    {
        new = TaxDto(){ServiceTypeId=ServiceType1, QuarterlyCharge =1, AnnualCharge=1}
    }
}

所以基本上您创建了一个从 IEnumerable<IGrouping<string, ServiceModel>> 到单个 ServiceModel 的映射。但是您似乎反而想为 IGrouping<string, ServiceModel> 创建一个映射到 ServiceModel.

所以将映射更改为:

CreateMap<IGrouping<string, ServiceModel>, ServiceResponseDto>()
  .ForMember(dest => dest.ServiceId, opt => opt.MapFrom(src => src.Key))
  .ForMember(dest => dest.ServiceName, opt => opt.MapFrom(src => src.FirstOrDefault().ServiceName))
  .ForMember(dest => dest.Tax, opt => opt.MapFrom(src => src.Select(c => new TaxDto() { ServiceTypeId = c.ServiceTypeId, QuarterlyCharge = c.QuarterlyCharge, AnnualCharge = c.AnnualCharge })));

然后在代码调用中:

var mappedServices = mapper.Map<IEnumerable<IGrouping<string, ServiceModel>>, IEnumerable<ServiceResponseDto>>(groupedServices);

请记住,既然你从 IEnumerable<IGrouping<string, ServiceModel>> 开始,你最终会得到 IEnumerable<ServiceResponseDto>

在此处查看实际效果: https://dotnetfiddle.net/YzIlDt

希望这对您有所帮助,祝您项目顺利!