You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用AutoMapper实现Product到ProductWithPricesResponse的嵌套列表映射?

问题:AutoMapper实现Product到ProductWithPricesResponse的嵌套列表映射

类定义

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public ProductStatus Status { get; set; }
    public List<ProductPrice> Prices { get; set; }
    public List<ProductStock> Stocks { get; set; }
}

public class ProductPrice
{
    public int Id { get; set; }
    public int ProductId { get; set; }        
    public decimal Price { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
}

public class ProductWithPricesResponse
{
    public string Name { get; set; }
    public string Description { get; set; }
    public List<ProductPriceResponse> Prices { get; set; }
}

public class ProductPriceResponse
{
    public decimal Price { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
}

问题描述

希望使用AutoMapper将Product映射到ProductWithPricesResponse,但不知道如何处理Product中ProductPrice列表到ProductWithPricesResponse中ProductPriceResponse列表的嵌套映射。

尝试了以下代码,出现错误:无法将ProductPriceResponse泛型列表隐式转换为ProductWithPricesResponse对象

public ProductProfile(IMapper mapper)
{
    _mapper = mapper;
    CreateMap<Product, ProductWithPricesResponse>()
        .ConvertUsing(x => x.Prices.Select(y => _mapper.Map<ProductPrice, ProductPriceResponse>(y)).ToList());
}

错误原因

你用ConvertUsing返回的是List<ProductPriceResponse>,但目标类型是ProductWithPricesResponse,类型不匹配导致报错。而且AutoMapper本身支持嵌套列表的自动映射,不需要手动遍历转换。

正确实现方法

方法1:AutoMapper自动映射(推荐)

由于实体和DTO的属性名称完全匹配(Name、Description、Prices,以及ProductPrice和ProductPriceResponse的Price、StartDate、EndDate),AutoMapper会自动识别并完成嵌套映射,只需定义两个基础映射:

public class ProductProfile : Profile
{
    public ProductProfile()
    {
        // 定义ProductPrice到ProductPriceResponse的映射
        CreateMap<ProductPrice, ProductPriceResponse>();
        // 定义Product到ProductWithPricesResponse的映射,AutoMapper自动处理Prices列表的嵌套转换
        CreateMap<Product, ProductWithPricesResponse>();
    }
}

方法2:显式指定列表映射(可选)

如果需要自定义映射逻辑,可以显式指定Prices属性的映射方式,无需使用ConvertUsing:

public class ProductProfile : Profile
{
    public ProductProfile()
    {
        CreateMap<ProductPrice, ProductPriceResponse>();
        
        CreateMap<Product, ProductWithPricesResponse>()
            // 显式指定Name的映射(可选,自动匹配可省略)
            .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
            // 显式指定Description的映射(可选)
            .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description))
            // 显式指定Prices列表的映射,AutoMapper会用已定义的映射处理每个元素
            .ForMember(dest => dest.Prices, opt => opt.MapFrom(src => src.Prices));
    }
}

注意事项

  • ConvertUsing要求返回目标对象(这里是ProductWithPricesResponse),不能返回其属性的列表类型。
  • 确保已在DI容器中正确注册AutoMapper(例如调用services.AddAutoMapper(typeof(ProductProfile));)。

内容的提问来源于stack exchange,提问作者iMSn20

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 12:23:17