简体   繁体   中英

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?

Try the following mapping configuration:

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

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