Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.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# 在同一行中的自动映射中初始化Ilist_C#_List_Automapper - Fatal编程技术网

C# 在同一行中的自动映射中初始化Ilist

C# 在同一行中的自动映射中初始化Ilist,c#,list,automapper,C#,List,Automapper,我似乎在类中定义IList时遇到了一些问题,使用automapper,然后将一个元素添加到此列表中 var config = new MapperConfiguration(.ForMember(dest => dest.Catalogs = new List<dto>); var config=new-MapperConfiguration(.ForMember(dest=>dest.Catalogs=new-List); 这不能在同一行中完成吗?您应该注意的第一件事是,

我似乎在类中定义IList时遇到了一些问题,使用automapper,然后将一个元素添加到此列表中

var config = new MapperConfiguration(.ForMember(dest => dest.Catalogs = new List<dto>);
var config=new-MapperConfiguration(.ForMember(dest=>dest.Catalogs=new-List);

这不能在同一行中完成吗?

您应该注意的第一件事是,如果AutoMapper知道如何映射这些集合的元素,它将自动创建集合

例如,给定以下类别:

public class Source {
    public IList<SourceObj> SourceCollection {get; set;}
}

public class Destination {
    public DestDto[] DestinationCollection {get; set;}
}
CreateMap<SourceObj, DestDto>();

CreateMap<Source, Destination>()
    .ForMember(dest => dest.DestinationCollection, opt => opt.MapFrom(src => src.SourceCollection));
var destDto = Mapper.Map<Destination>(sourceObj);
Assert.IsNotNull(destDto.DestinationCollection);
CreateMap<Source, Destination>()
    .ForMember(dest => dest.DestinationCollection, opt => opt.ResolveUsing(src => new List<DestDto>()));