我发现使用AutoMapper从我的数据源映射到目标很困难。 我有一个包含对象列表的源类。 现在,我想使用foreach迭代通过列表以将该列表映射到单个类中,以便使用Automapper填充该类。
public class Source
{
public List<StudentName> studentName {get; set;}
}
public class StudentName
{
public string Name { get; set;}
}
public class Destination
{
public string FirstName { get; set;}
}
public List<Destination> GetStud(Source source)
{
MapperConfigurationExpression cfg = new MapperConfigurationExpression();
cfg.ValidateInlineMaps = false;
cfg.CreateMap<Source, Destination>();
MapperConfiguration mapperConfig = new MapperConfiguration(cfg);
IMapper mapper = new Mapper(mapperConfig);
var viewModel= new List<Destination>();
// enter code here
foreach (var item in source.studentName)
{
var destination = new Destination();
destination.FirstName = item.Name;
destination = mapper.Map<Destination>(item);
viewModel.Add(destination);
var man = 0;
}
return viewModel;
}
我尝试的这种方法给我返回了以下错误:未映射的成员被发现。请查看下面的类型和成员。添加自定义映射表达式、忽略、添加自定义解析器或修改源/目标类型。如果没有匹配的构造函数,请添加无参构造函数、添加可选参数或映射所有构造函数参数。