lapsus
lapsus

Reputation: 3035

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.

Upvotes: 0

Views: 325

Answers (1)

Lucian Bargaoanu
Lucian Bargaoanu

Reputation: 3516

Remove that converter. And try

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

Upvotes: 1

Related Questions