lonelydev101
lonelydev101

Reputation: 1911

How to map enum list to string list with automapper

I have class A and class B:

class A
{
    public List<TypeEnum> Types { get; set; }
    ...

}

class B
{
    public List<string> TypesString { get; set; }
    ...
}

I am trying to map List to List:

CreateMap<ClassA, CLassB>() .ForMemebr(destination => destination.TypesString, m => m.ConvertUsing(source => ((byte)source.Types).ToString()))

Outcome: Desired outcome should be successful mapping from List of enums to List of strings for example: List<string> => ["1","2","3","5"]

How can I do that?

Upvotes: 0

Views: 1193

Answers (1)

Sergey Anisimov
Sergey Anisimov

Reputation: 895

Try the following mapping configuration:

CreateMap<ClassA, CLassB>().ForMember(destination => destination.TypesString, 
                                      opt => opt.MapFrom(s => s.Types.Select(x => ((byte)x))));

Upvotes: 3

Related Questions