Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Automapper-将对象列表映射到具有嵌套对象的单个复杂类型_C#_Asp.net Core_.net Core_Automapper - Fatal编程技术网

C# Automapper-将对象列表映射到具有嵌套对象的单个复杂类型

C# Automapper-将对象列表映射到具有嵌套对象的单个复杂类型,c#,asp.net-core,.net-core,automapper,C#,Asp.net Core,.net Core,Automapper,我正在使用一个自动映射器,我需要将一个对象列表映射到一个复杂类型,该类型有很多嵌套对象,但我找不到正确的方法。当然,我有很多具体的东西,但我只是在简化我的情况 资料来源: 目的地: 我收到了服务部门的回复: public List<SourceBase> Foo { get; set; } 谢谢大家! 基本上我已经用Linq编写了一个自定义映射程序 CreateMap<List<SourceBase>, DestObj>() .ForMember(d

我正在使用一个自动映射器,我需要将一个对象列表映射到一个复杂类型,该类型有很多嵌套对象,但我找不到正确的方法。当然,我有很多具体的东西,但我只是在简化我的情况

资料来源:

目的地:

我收到了服务部门的回复:

public List<SourceBase> Foo { get; set; }

谢谢大家!

基本上我已经用Linq编写了一个自定义映射程序

CreateMap<List<SourceBase>, DestObj>()
    .ForMember(dest => dest.Dest1Obj, opt => opt.MapFrom(src => src.Single(x => x.GetType() == typeof(Source1))))
    .ForMember(dest => dest.Dest2Obj, opt => opt.MapFrom(src => src.Single(x => x.GetType() == typeof(Source2))));

CreateMap<Source1, Dest1>();
CreateMap<Source2, Dest2>();

为什么呢?你有SourceBase的列表吗?为什么不把它映射到DestBase列表中呢?因为我们想让前端视图模型更简单
public List<SourceBase> Foo { get; set; }
public class DestObj 
{
    public Dest1 Dest1Obj { get; set; }
    public Dest2 Dest2Obj { get; set; }
}
CreateMap<List<SourceBase>, DestObj>()
    .ForMember(dest => dest.Dest1Obj, opt => opt.MapFrom(src => src.Single(x => x.GetType() == typeof(Source1))))
    .ForMember(dest => dest.Dest2Obj, opt => opt.MapFrom(src => src.Single(x => x.GetType() == typeof(Source2))));

CreateMap<Source1, Dest1>();
CreateMap<Source2, Dest2>();