C# 使用Automapper 5是否可以将多个属性映射到一个列表中?

C# 使用Automapper 5是否可以将多个属性映射到一个列表中?,c#,automapper,automapper-5,C#,Automapper,Automapper 5,我不是以英语为母语的,所以如果有重复的问题,我深表歉意 我有一个请求类: class input { Car mainCar, List<Car> otherCars } 类输入{ 汽车主机, 列出其他汽车 } 要映射到: class mapped { List<CarDTO> cars } 类映射{ 列出汽车 } 使用类似于从mainCar映射时的选项设置carType=EnumCarType.Main,else EnumCarType.Other

我不是以英语为母语的,所以如果有重复的问题,我深表歉意

我有一个请求类:

class input {
  Car mainCar,
  List<Car> otherCars
}
类输入{
汽车主机,
列出其他汽车
}
要映射到:

class mapped {
  List<CarDTO> cars
}
类映射{
列出汽车
}
使用类似于从mainCar映射时的选项设置carType=EnumCarType.Main,else EnumCarType.Other


这对Automapper 5有用吗?

这段代码应该可以让您开始使用,尽管有些细节还不清楚,而且我这里没有编译器:它做出合理的假设,并使用了一个。注册后,无论何时从输入对象映射到映射对象,它都用于执行实际转换

public class CarTypeConverter : ITypeConverter<input, mapped> 
{
    public mapped Convert(ResolutionContext context) 
    {
        // get the input object from the context
        input inputCar = (input)context.SourceValue;

        // get the main car        
        CarDTO mappedMainCar = Mapper.Map<Car, CarDTO>(input.mainCar);
        mappedMainCar.carType = EnumCarType.Main;

        // create a list with the main car, then add the rest
        var mappedCars = new List<CarDTO> { mappedMainCar };
        mappedCars.AddRange(Mapper.Map<Car, CarDTO>(inputCar.otherCars));

        return new mapped { cars = mappedCars };
    }
}

// In Automapper initialization
mapperCfg.CreateMap<input, mapped>().ConvertUsing<CarTypeConverter>();
mapperCfg.CreateMap<Car, CarDTO>()
           .ForMember(dest => dest.carType, opt => EnumCarType.Other);
公共类CarTypeConverter:ITypeConverter
{
公共映射转换(ResolutionContext上下文)
{
//从上下文中获取输入对象
input inputCar=(输入)context.SourceValue;
//去拿主车
CarDTO mappedMainCar=Mapper.Map(input.mainCar);
mappedMainCar.carType=EnumCarType.Main;
//用主车创建一个列表,然后添加其余的
var mappedCars=新列表{mappedMainCar};
mappedCars.AddRange(Mapper.Map(inputCar.otherCars));
返回新映射的{cars=mappedCars};
}
}
//在自动映射初始化中
mapperCfg.CreateMap().ConvertUsing();
mapperpcfg.CreateMap()
.ForMember(dest=>dest.carType,opt=>EnumCarType.Other);

这段代码应该会让你开始学习,虽然有些细节还不清楚,而且我这里没有编译器:它做出合理的假设,并使用了一个。注册后,无论何时从输入对象映射到映射对象,它都用于执行实际转换

public class CarTypeConverter : ITypeConverter<input, mapped> 
{
    public mapped Convert(ResolutionContext context) 
    {
        // get the input object from the context
        input inputCar = (input)context.SourceValue;

        // get the main car        
        CarDTO mappedMainCar = Mapper.Map<Car, CarDTO>(input.mainCar);
        mappedMainCar.carType = EnumCarType.Main;

        // create a list with the main car, then add the rest
        var mappedCars = new List<CarDTO> { mappedMainCar };
        mappedCars.AddRange(Mapper.Map<Car, CarDTO>(inputCar.otherCars));

        return new mapped { cars = mappedCars };
    }
}

// In Automapper initialization
mapperCfg.CreateMap<input, mapped>().ConvertUsing<CarTypeConverter>();
mapperCfg.CreateMap<Car, CarDTO>()
           .ForMember(dest => dest.carType, opt => EnumCarType.Other);
公共类CarTypeConverter:ITypeConverter
{
公共映射转换(ResolutionContext上下文)
{
//从上下文中获取输入对象
input inputCar=(输入)context.SourceValue;
//去拿主车
CarDTO mappedMainCar=Mapper.Map(input.mainCar);
mappedMainCar.carType=EnumCarType.Main;
//用主车创建一个列表,然后添加其余的
var mappedCars=新列表{mappedMainCar};
mappedCars.AddRange(Mapper.Map(inputCar.otherCars));
返回新映射的{cars=mappedCars};
}
}
//在自动映射初始化中
mapperCfg.CreateMap().ConvertUsing();
mapperpcfg.CreateMap()
.ForMember(dest=>dest.carType,opt=>EnumCarType.Other);