如果我有一个对象列表,如下所示:
//There is a class just like this InnerSource
public class InnerClass
{
//Because why not
public int[] Nums;
}
//There is a class OuterSource just like this but it has a List of
//InnerSource objects
public class OuterClass
{
public IEnumerable<InnerClass> InnerObjects;
}
我认为这是处理将OuterSource对象映射到OuterClass对象的优雅方法,但它似乎不起作用:
public class CreateMaps
{
public void CreateAllMaps()
{
Mapper.CreateMap<InnerSource, InnerClass>();
Mapper.CreateMap<OuterSource, OuterClass>()
.ForMember(dest => dest.InnerObjects, opt => MapFrom(
src => src.InnerSource));
}
}
我也尝试过使用src =&gt; Mapper.Map,IEnumerable&gt;(src.InnerSource)但是也没有用。由于没有工作,我的意思是,我得到一个空列表而不是获取InnerClass对象的列表。我确保InnerSource列表中有对象,所以我真的很茫然。有什么想法吗?
答案 0 :(得分:0)
我实际上认为你甚至不需要ForMember
行。试着这样做:
public class CreateMaps
{
public void CreateAllMaps()
{
Mapper.CreateMap<InnerSource, InnerClass>();
Mapper.CreateMap<OuterSource, OuterClass>();
}
}