我发现很难使用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;
}
我尝试过的这种方法给我以下错误 找到未映射的成员。在下面查看类型和成员。 添加自定义映射表达式,忽略,添加自定义解析器或修改源/目标类型 对于没有匹配的构造函数,请添加一个无参数ctor,添加可选参数,或映射所有构造函数参数
答案 0 :(得分:1)
您必须分配要映射的属性。没必要做foreach,映射到List:
Inductive BraunTree : Type :=
| E : BraunTree
| T: V -> BraunTree -> BraunTree -> BraunTree.