简体   繁体   中英

Automapper map source property of type A to destination List<B>

My problem is that I need to both convert type A to type B (and all nested types!) and also convert a single object ('A') to a List of objects ('B') at the same time.

public class SourcePoco
{
    public ComplexTypeA MyProblem { get; set; }
    // there be more properties...
}

public class ComplexTypeA
{
    // ...more nested complex types
}

My question now is how to I map MyProblem to the following destination type:

public class DestinationPoco
{
    public IEnumerable<ComplexTypeB> MyProblems { get; set; }
    // there be more properties...
}

I do have the following mappings:

CreateMap<SourcePoco, DestinationPoco>()
   .ForMember(...);

CreateMap<ComplexTypeA , ComplexTypeB>()
   .ForMember(dest => dest.Id, opt => opt.Ignore());

CreateMap<ComplexTypeA, IEnumerable<ComplexTypeB>>()
    .ConvertUsing<MyProblemConverter>();

I tried adding something like that - but it never gets invoked.

Remove that converter. And try

CreateMap<SourcePoco, DestinationPoco>().ForMember(d=>MyProblems,o=>o.MapFrom(s=>new[]{s.MyProblem}));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM