Automapper - 将多个属性从不同 类 映射到单个属性

Automapper - Mapping multiple properties from different classes to single one

我有 3 个来源 classes

public class ProductModel
{
    public ProductModel(string modelNo)
    {
        ModelNo = modelNo;
        Styles = new List<ProductStyle>();
    }

    public string ModelNo { get; set; }
    public List<ProductStyle> Styles { get; set; }
}

public class ProductStyle
{
    public ProductStyle(string color)
    {
        Color = color;
        Variants = new List<ProductVariant>();
    }

    public string Color { get; set; }
    public List<ProductVariant> Variants { get; set; }

}

public class ProductVariant
{
    public ProductVariant(string size)
    {
        Size = size;
    }

    public string Size { get; set; }
}

public class Inventory //2021.10.13 11:30
{
    public string ModelNo { get; set; }
    public decimal Quantity { get; set; }
}

这是我的目标class

public class Product
{
    public string Code { get; set; }
    public List<ProductStock> {get;set;} //added new property
}

public class ProductStock //2021.10.13 11:30
{
    public decimal Quantity { get; set; }
}

每个模型都有风格,每个风格都有变体。

我必须以这样的方式映射它们,使产品的结果代码包含具有以下命名约定的上述属性

Code = String.Concat(ModelNo,"#",Color,"#",Size)

我的映射器看起来像这样:

    public void Mapper()
    {
        foreach (var model in this.Data())
        {
            foreach (var style in model.Styles)
            {
                foreach (var variant in style.Variants)
                {
                    //our products are here
                    Product product = 
                                  _mapper.Map(model,
                                    _mapper.Map(style,
                                      _mapper.Map<ProductVariant, Product>(variant)));
                    products.Add(product);
                }
            }
        }
    }

这是一些数据:

    public static void Data()
    {
        List<ProductModel> models = new List<ProductModel>();

        var variant1 = new ProductVariant("L");
        var variant2 = new ProductVariant("M");
        var variant3 = new ProductVariant("S");

        var style1 = new ProductStyle("BLUE");
        var style2 = new ProductStyle("RED"); ;

        style1.Variants.Add(variant1);
        style2.Variants.Add(variant2);
        style2.Variants.Add(variant3);

        var model1 = new ProductModel("model1");

        model1.Styles.Add(style1);
        model1.Styles.Add(style2);
    }

请给我一些建议,我的构造函数应该是什么样子 class 才能将多个属性正确映射到一个属性。

我这样试过,我每次都覆盖字符串代码,但这不起作用。

public class TestProfile : Profile
{
    public TestProfile()
    {
        var model = CreateMap<ProductModel, Product>()
       .ForMember(o => o.Code, o => o.MapFrom(m => m.ModelNo));
       //.AfterMap((s, entity) =>
       // {
       //     entity.Code = String.Concat(entity.Code, "-", s.ModelNo);
       // });

        var style = CreateMap<ProductStyle, Product>()
        .AfterMap((v, entity) =>
        {
            entity.Code = String.Concat(entity.Code, "-", v.Color);
        });

        var variant = CreateMap<ProductVariant, Product>()
        .AfterMap((v, entity) =>
        {
            entity.Code = String.Concat(entity.Code, "-", v.Size);
        });

    }
}

Custom Type Convertor 中使用 LINQ 表达式。

每个ProductModel都可以转化为List<Product>。所以你不能在 ProductModelProduct.

之间创建映射

映射器配置文件

CreateMap<List<ProductModel>, List<Product>>()
.ConvertUsing(p => p.SelectMany(m => m.Styles
                               .SelectMany(s => s.Variants
                                .Select(v => new Product() { Code = String.Concat(m.ModelNo, "#", s.Color, "#", v.Size) })))
                                 .ToList());

代码

.............
.............
models.Add(model1);
models.Add(model2);
var result = _mapper.Map<List<Product>>(models);